Skip to content

Commit

Permalink
Add version information
Browse files Browse the repository at this point in the history
  • Loading branch information
breml committed Feb 8, 2022
1 parent 2045751 commit f84723e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Expand Up @@ -29,3 +29,5 @@ release:
github:
owner: breml
name: bidichk
gomod:
proxy: true
31 changes: 31 additions & 0 deletions cmd/bidichk/main.go
@@ -1,11 +1,42 @@
package main

import (
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/debug"

"golang.org/x/tools/go/analysis/singlechecker"

"github.com/breml/bidichk/pkg/bidichk"
)

var (
version = "development"
commit = ""
date = ""
)

func main() {
bidichk.Version = buildVersion()

singlechecker.Main(bidichk.NewAnalyzer())
}

func buildVersion() string {
result := fmt.Sprintf("%s version %s", filepath.Base(os.Args[0]), version)

if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
}
result = fmt.Sprintf("%s\ngoos: %s\ngoarch: %s", result, runtime.GOOS, runtime.GOARCH)

return result
}
1 change: 1 addition & 0 deletions pkg/bidichk/bidichk.go
Expand Up @@ -136,6 +136,7 @@ func NewAnalyzer() *analysis.Analyzer {

a.Flags.Init("bidichk", flag.ExitOnError)
a.Flags.Var(&bidichk.disallowedRunes, "disallowed-runes", disallowedDoc)
a.Flags.Var(versionFlag{}, "V", "print version and exit")

return a
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/bidichk/version.go
@@ -0,0 +1,19 @@
package bidichk

import (
"fmt"
"os"
)

var Version = "bidichk version dev"

type versionFlag struct{}

func (versionFlag) IsBoolFlag() bool { return true }
func (versionFlag) Get() interface{} { return nil }
func (versionFlag) String() string { return "" }
func (versionFlag) Set(s string) error {
fmt.Println(Version)
os.Exit(0)
return nil
}

0 comments on commit f84723e

Please sign in to comment.