Skip to content

Commit

Permalink
Merge pull request #12 from breml/issue_10
Browse files Browse the repository at this point in the history
Add version information
  • Loading branch information
breml authored Feb 8, 2022
2 parents f4f2a89 + 4f11bb1 commit 3aae218
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ release:
github:
owner: breml
name: errchkjson
gomod:
proxy: true
31 changes: 31 additions & 0 deletions cmd/errchkjson/main.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions errchkjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3aae218

Please sign in to comment.