diff --git a/.goreleaser.yml b/.goreleaser.yml index 8ed3ee2..5f23690 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -29,3 +29,5 @@ release: github: owner: breml name: errchkjson +gomod: + proxy: true diff --git a/cmd/errchkjson/main.go b/cmd/errchkjson/main.go index 761d3c7..643951d 100644 --- a/cmd/errchkjson/main.go +++ b/cmd/errchkjson/main.go @@ -1,11 +1,42 @@ package main import ( + "fmt" + "os" + "path/filepath" + "runtime" + "runtime/debug" + "github.com/breml/errchkjson" "golang.org/x/tools/go/analysis/singlechecker" ) +var ( + version = "development" + commit = "" + date = "" +) + func main() { + errchkjson.Version = buildVersion() + singlechecker.Main(errchkjson.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 +} diff --git a/errchkjson.go b/errchkjson.go index d3c0b71..30d8ffd 100644 --- a/errchkjson.go +++ b/errchkjson.go @@ -32,6 +32,7 @@ func NewAnalyzer() *analysis.Analyzer { a.Flags.Init("errchkjson", flag.ExitOnError) a.Flags.BoolVar(&errchkjson.omitSafe, "omit-safe", false, "if omit-safe is true, checking of safe returns is omitted") a.Flags.BoolVar(&errchkjson.reportNoExported, "report-no-exported", false, "if report-no-exported is true, encoding a struct without exported fields is reported as issue") + a.Flags.Var(versionFlag{}, "V", "print version and exit") return a } diff --git a/version.go b/version.go new file mode 100644 index 0000000..77d8ef8 --- /dev/null +++ b/version.go @@ -0,0 +1,19 @@ +package errchkjson + +import ( + "fmt" + "os" +) + +var Version = "errchkjson 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 +}