Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version information #12

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}