Skip to content

Commit 6287c56

Browse files
committed
lintcmd: allow setting the version
1 parent 24e1ffd commit 6287c56

File tree

8 files changed

+39
-23
lines changed

8 files changed

+39
-23
lines changed

cmd/keyify/keyify.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454
flag.Parse()
5555

5656
if fVersion {
57-
version.Print()
57+
version.Print(version.Version, version.MachineVersion)
5858
os.Exit(0)
5959
}
6060

cmd/staticcheck/staticcheck.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
"honnef.co/go/tools/lintcmd"
9+
"honnef.co/go/tools/lintcmd/version"
910
"honnef.co/go/tools/quickfix"
1011
"honnef.co/go/tools/simple"
1112
"honnef.co/go/tools/staticcheck"
@@ -15,6 +16,7 @@ import (
1516

1617
func main() {
1718
cmd := lintcmd.NewCommand("staticcheck")
19+
cmd.SetVersion(version.Version, version.MachineVersion)
1820

1921
fs := cmd.FlagSet()
2022
debug := fs.String("debug.unused-graph", "", "Write unused's object graph to `file`")

cmd/structlayout-optimize/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232
flag.Parse()
3333

3434
if fVersion {
35-
version.Print()
35+
version.Print(version.Version, version.MachineVersion)
3636
os.Exit(0)
3737
}
3838

cmd/structlayout-pretty/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929
flag.Parse()
3030

3131
if fVersion {
32-
version.Print()
32+
version.Print(version.Version, version.MachineVersion)
3333
os.Exit(0)
3434
}
3535

cmd/structlayout/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232
flag.Parse()
3333

3434
if fVersion {
35-
version.Print()
35+
version.Print(version.Version, version.MachineVersion)
3636
os.Exit(0)
3737
}
3838

lintcmd/cmd.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -568,20 +568,35 @@ func (list *list) Set(s string) error {
568568

569569
// Command represents a linter command line tool.
570570
type Command struct {
571-
name string
572-
flags *flag.FlagSet
573-
analyzers map[string]*lint.Analyzer
571+
name string
572+
flags *flag.FlagSet
573+
analyzers map[string]*lint.Analyzer
574+
version string
575+
machineVersion string
574576
}
575577

576578
// NewCommand returns a new Command.
577579
func NewCommand(name string) *Command {
578580
return &Command{
579-
name: name,
580-
flags: flagSet(name),
581-
analyzers: map[string]*lint.Analyzer{},
581+
name: name,
582+
flags: flagSet(name),
583+
analyzers: map[string]*lint.Analyzer{},
584+
version: "devel",
585+
machineVersion: "devel",
582586
}
583587
}
584588

589+
// SetVersion sets the command's version.
590+
// It is divided into a human part and a machine part.
591+
// For example, Staticcheck 2020.2.1 had the human version "2020.2.1" and the machine version "v0.1.1".
592+
// If you only use Semver, you can set both parts to the same value.
593+
//
594+
// Calling this method is optional. Both versions default to "devel", and we'll attempt to deduce more version information from the Go module.
595+
func (cmd *Command) SetVersion(human, machine string) {
596+
cmd.version = human
597+
cmd.machineVersion = machine
598+
}
599+
585600
// FlagSet returns the command's flag set.
586601
// This can be used to add additional command line arguments.
587602
func (cmd *Command) FlagSet() *flag.FlagSet {
@@ -750,7 +765,7 @@ func (cmd *Command) Run() {
750765
}
751766

752767
if debugVersion {
753-
version.Verbose()
768+
version.Verbose(cmd.version, cmd.machineVersion)
754769
exit(0)
755770
}
756771

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

776791
if printVersion {
777-
version.Print()
792+
version.Print(cmd.version, cmd.machineVersion)
778793
exit(0)
779794
}
780795

lintcmd/format.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"text/tabwriter"
1111

1212
"honnef.co/go/tools/analysis/lint"
13-
"honnef.co/go/tools/lintcmd/version"
1413
"honnef.co/go/tools/sarif"
1514
)
1615

@@ -174,11 +173,11 @@ func (o *sarifFormatter) Start(checks []*lint.Analyzer) {
174173
Driver: sarif.ToolComponent{
175174
Name: "Staticcheck",
176175
// XXX version.Version is useless when it's "devel"
177-
Version: version.Version,
176+
// Version: version.Version, // XXX pass this through from the command
178177
// XXX SemanticVersion is useless when it's "devel"
179178
// XXX SemanticVersion shouldn't have the leading "v"
180-
SemanticVersion: version.MachineVersion,
181-
InformationURI: "https://staticcheck.io",
179+
// SemanticVersion: version.MachineVersion, // XXX pass this through from the command
180+
InformationURI: "https://staticcheck.io",
182181
},
183182
},
184183
Invocations: []sarif.Invocation{{

lintcmd/version/version.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const MachineVersion = "devel"
1212

1313
// version returns a version descriptor and reports whether the
1414
// version is a known release.
15-
func version() (human, machine string, known bool) {
16-
if Version != "devel" {
17-
return Version, MachineVersion, true
15+
func version(human, machine string) (human_, machine_ string, known bool) {
16+
if human != "devel" {
17+
return human, machine, true
1818
}
1919
v, ok := buildInfoVersion()
2020
if ok {
@@ -23,8 +23,8 @@ func version() (human, machine string, known bool) {
2323
return "devel", "", false
2424
}
2525

26-
func Print() {
27-
human, machine, release := version()
26+
func Print(human, machine string) {
27+
human, machine, release := version(human, machine)
2828

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

38-
func Verbose() {
39-
Print()
38+
func Verbose(human, machine string) {
39+
Print(human, machine)
4040
fmt.Println()
4141
fmt.Println("Compiled with Go version:", runtime.Version())
4242
printBuildInfo()

0 commit comments

Comments
 (0)