Skip to content

Commit d9f636b

Browse files
committed
dev: add Go version to version information
1 parent 610a2bd commit d9f636b

File tree

5 files changed

+52
-39
lines changed

5 files changed

+52
-39
lines changed

cmd/golangci-lint/main.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,43 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"runtime/debug"
67

78
"github.com/golangci/golangci-lint/pkg/commands"
89
"github.com/golangci/golangci-lint/pkg/exitcodes"
910
)
1011

1112
var (
13+
goVersion = "unknown"
14+
1215
// Populated by goreleaser during build
1316
version = "master"
1417
commit = "?"
1518
date = ""
1619
)
1720

21+
//nolint:gochecknoinits
22+
func init() {
23+
if info, available := debug.ReadBuildInfo(); available {
24+
goVersion = info.GoVersion
25+
26+
if date == "" {
27+
version = info.Main.Version
28+
commit = fmt.Sprintf("(unknown, mod sum: %q)", info.Main.Sum)
29+
date = "(unknown)"
30+
}
31+
}
32+
}
33+
1834
func main() {
19-
e := commands.NewExecutor(version, commit, date)
35+
info := commands.BuildInfo{
36+
GoVersion: goVersion,
37+
Version: version,
38+
Commit: commit,
39+
Date: date,
40+
}
41+
42+
e := commands.NewExecutor(info)
2043

2144
if err := e.Execute(); err != nil {
2245
fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err)

cmd/golangci-lint/mod_version.go

-17
This file was deleted.

pkg/commands/executor.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ import (
3030
"github.com/golangci/golangci-lint/pkg/timeutils"
3131
)
3232

33+
type BuildInfo struct {
34+
GoVersion string `json:"goVersion"`
35+
Version string `json:"version"`
36+
Commit string `json:"commit"`
37+
Date string `json:"date"`
38+
}
39+
3340
type Executor struct {
3441
rootCmd *cobra.Command
3542
runCmd *cobra.Command
3643
lintersCmd *cobra.Command
3744

38-
exitCode int
39-
version, commit, date string
45+
exitCode int
46+
buildInfo BuildInfo
4047

4148
cfg *config.Config // cfg is the unmarshaled data from the golangci config file.
4249
log logutils.Log
@@ -56,13 +63,11 @@ type Executor struct {
5663
}
5764

5865
// NewExecutor creates and initializes a new command executor.
59-
func NewExecutor(version, commit, date string) *Executor {
66+
func NewExecutor(buildInfo BuildInfo) *Executor {
6067
startedAt := time.Now()
6168
e := &Executor{
6269
cfg: config.NewDefault(),
63-
version: version,
64-
commit: commit,
65-
date: date,
70+
buildInfo: buildInfo,
6671
DBManager: lintersdb.NewManager(nil, nil),
6772
debugf: logutils.Debug(logutils.DebugKeyExec),
6873
}
@@ -135,7 +140,7 @@ func NewExecutor(version, commit, date string) *Executor {
135140
e.loadGuard = load.NewGuard()
136141
e.contextLoader = lint.NewContextLoader(e.cfg, e.log.Child(logutils.DebugKeyLoader), e.goenv,
137142
e.lineCache, e.fileCache, e.pkgCache, e.loadGuard)
138-
if err = e.initHashSalt(version); err != nil {
143+
if err = e.initHashSalt(buildInfo.Version); err != nil {
139144
e.log.Fatalf("Failed to init hash salt: %s", err)
140145
}
141146
e.debugf("Initialized executor in %s", time.Since(startedAt))

pkg/commands/root.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424

2525
func (e *Executor) persistentPreRun(_ *cobra.Command, _ []string) error {
2626
if e.cfg.Run.PrintVersion {
27-
_, _ = fmt.Fprintf(logutils.StdOut, "golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
27+
_ = printVersion(logutils.StdOut, e.buildInfo)
2828
os.Exit(exitcodes.Success) // a return nil is not enough to stop the process because we are inside the `preRun`.
2929
}
3030

@@ -145,7 +145,7 @@ func (e *Executor) initRoot() {
145145
}
146146

147147
func (e *Executor) needVersionOption() bool {
148-
return e.date != ""
148+
return e.buildInfo.Date != ""
149149
}
150150

151151
func initRootFlagSet(fs *pflag.FlagSet, cfg *config.Config, needVersionOption bool) {

pkg/commands/version.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os"
78
"strings"
89

@@ -13,9 +14,10 @@ import (
1314
)
1415

1516
type jsonVersion struct {
16-
Version string `json:"version"`
17-
Commit string `json:"commit"`
18-
Date string `json:"date"`
17+
GoVersion string `json:"goVersion"`
18+
Version string `json:"version"`
19+
Commit string `json:"commit"`
20+
Date string `json:"date"`
1921
}
2022

2123
func (e *Executor) initVersionConfiguration(cmd *cobra.Command) {
@@ -39,24 +41,24 @@ func (e *Executor) initVersion() {
3941
RunE: func(cmd *cobra.Command, _ []string) error {
4042
switch strings.ToLower(e.cfg.Version.Format) {
4143
case "short":
42-
fmt.Println(e.version)
44+
fmt.Println(e.buildInfo.Version)
4345
return nil
4446

4547
case "json":
46-
ver := jsonVersion{
47-
Version: e.version,
48-
Commit: e.commit,
49-
Date: e.date,
50-
}
51-
return json.NewEncoder(os.Stdout).Encode(&ver)
48+
return json.NewEncoder(os.Stdout).Encode(e.buildInfo)
5249

5350
default:
54-
fmt.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
55-
return nil
51+
return printVersion(os.Stdout, e.buildInfo)
5652
}
5753
},
5854
}
5955

6056
e.rootCmd.AddCommand(versionCmd)
6157
e.initVersionConfiguration(versionCmd)
6258
}
59+
60+
func printVersion(w io.Writer, buildInfo BuildInfo) error {
61+
_, err := fmt.Fprintf(w, "golangci-lint has version %s built with %s from %s on %s\n",
62+
buildInfo.Version, buildInfo.GoVersion, buildInfo.Commit, buildInfo.Date)
63+
return err
64+
}

0 commit comments

Comments
 (0)