Skip to content

Commit

Permalink
lintcmd: allow setting the version
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikh committed May 21, 2021
1 parent 24e1ffd commit 6287c56
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/keyify/keyify.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
flag.Parse()

if fVersion {
version.Print()
version.Print(version.Version, version.MachineVersion)
os.Exit(0)
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/staticcheck/staticcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"honnef.co/go/tools/lintcmd"
"honnef.co/go/tools/lintcmd/version"
"honnef.co/go/tools/quickfix"
"honnef.co/go/tools/simple"
"honnef.co/go/tools/staticcheck"
Expand All @@ -15,6 +16,7 @@ import (

func main() {
cmd := lintcmd.NewCommand("staticcheck")
cmd.SetVersion(version.Version, version.MachineVersion)

fs := cmd.FlagSet()
debug := fs.String("debug.unused-graph", "", "Write unused's object graph to `file`")
Expand Down
2 changes: 1 addition & 1 deletion cmd/structlayout-optimize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
flag.Parse()

if fVersion {
version.Print()
version.Print(version.Version, version.MachineVersion)
os.Exit(0)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/structlayout-pretty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
flag.Parse()

if fVersion {
version.Print()
version.Print(version.Version, version.MachineVersion)
os.Exit(0)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/structlayout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
flag.Parse()

if fVersion {
version.Print()
version.Print(version.Version, version.MachineVersion)
os.Exit(0)
}

Expand Down
31 changes: 23 additions & 8 deletions lintcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,20 +568,35 @@ func (list *list) Set(s string) error {

// Command represents a linter command line tool.
type Command struct {
name string
flags *flag.FlagSet
analyzers map[string]*lint.Analyzer
name string
flags *flag.FlagSet
analyzers map[string]*lint.Analyzer
version string
machineVersion string
}

// NewCommand returns a new Command.
func NewCommand(name string) *Command {
return &Command{
name: name,
flags: flagSet(name),
analyzers: map[string]*lint.Analyzer{},
name: name,
flags: flagSet(name),
analyzers: map[string]*lint.Analyzer{},
version: "devel",
machineVersion: "devel",
}
}

// SetVersion sets the command's version.
// It is divided into a human part and a machine part.
// For example, Staticcheck 2020.2.1 had the human version "2020.2.1" and the machine version "v0.1.1".
// If you only use Semver, you can set both parts to the same value.
//
// Calling this method is optional. Both versions default to "devel", and we'll attempt to deduce more version information from the Go module.
func (cmd *Command) SetVersion(human, machine string) {
cmd.version = human
cmd.machineVersion = machine
}

// FlagSet returns the command's flag set.
// This can be used to add additional command line arguments.
func (cmd *Command) FlagSet() *flag.FlagSet {
Expand Down Expand Up @@ -750,7 +765,7 @@ func (cmd *Command) Run() {
}

if debugVersion {
version.Verbose()
version.Verbose(cmd.version, cmd.machineVersion)
exit(0)
}

Expand All @@ -774,7 +789,7 @@ func (cmd *Command) Run() {
}

if printVersion {
version.Print()
version.Print(cmd.version, cmd.machineVersion)
exit(0)
}

Expand Down
7 changes: 3 additions & 4 deletions lintcmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"text/tabwriter"

"honnef.co/go/tools/analysis/lint"
"honnef.co/go/tools/lintcmd/version"
"honnef.co/go/tools/sarif"
)

Expand Down Expand Up @@ -174,11 +173,11 @@ func (o *sarifFormatter) Start(checks []*lint.Analyzer) {
Driver: sarif.ToolComponent{
Name: "Staticcheck",
// XXX version.Version is useless when it's "devel"
Version: version.Version,
// Version: version.Version, // XXX pass this through from the command
// XXX SemanticVersion is useless when it's "devel"
// XXX SemanticVersion shouldn't have the leading "v"
SemanticVersion: version.MachineVersion,
InformationURI: "https://staticcheck.io",
// SemanticVersion: version.MachineVersion, // XXX pass this through from the command
InformationURI: "https://staticcheck.io",
},
},
Invocations: []sarif.Invocation{{
Expand Down
14 changes: 7 additions & 7 deletions lintcmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const MachineVersion = "devel"

// version returns a version descriptor and reports whether the
// version is a known release.
func version() (human, machine string, known bool) {
if Version != "devel" {
return Version, MachineVersion, true
func version(human, machine string) (human_, machine_ string, known bool) {
if human != "devel" {
return human, machine, true
}
v, ok := buildInfoVersion()
if ok {
Expand All @@ -23,8 +23,8 @@ func version() (human, machine string, known bool) {
return "devel", "", false
}

func Print() {
human, machine, release := version()
func Print(human, machine string) {
human, machine, release := version(human, machine)

if release {
fmt.Printf("%s %s (%s)\n", filepath.Base(os.Args[0]), human, machine)
Expand All @@ -35,8 +35,8 @@ func Print() {
}
}

func Verbose() {
Print()
func Verbose(human, machine string) {
Print(human, machine)
fmt.Println()
fmt.Println("Compiled with Go version:", runtime.Version())
printBuildInfo()
Expand Down

0 comments on commit 6287c56

Please sign in to comment.