Skip to content

Commit e5b3064

Browse files
authored
dev: add Go version to version information (golangci#3625)
1 parent 3443b7a commit e5b3064

File tree

6 files changed

+70
-40
lines changed

6 files changed

+70
-40
lines changed

cmd/golangci-lint/main.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@ 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

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

2141
if err := e.Execute(); err != nil {
2242
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

+34-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package commands
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os"
8+
"runtime/debug"
79
"strings"
810

911
"github.com/spf13/cobra"
@@ -12,10 +14,9 @@ import (
1214
"github.com/golangci/golangci-lint/pkg/config"
1315
)
1416

15-
type jsonVersion struct {
16-
Version string `json:"version"`
17-
Commit string `json:"commit"`
18-
Date string `json:"date"`
17+
type versionInfo struct {
18+
Info BuildInfo
19+
BuildInfo *debug.BuildInfo
1920
}
2021

2122
func (e *Executor) initVersionConfiguration(cmd *cobra.Command) {
@@ -28,6 +29,7 @@ func initVersionFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
2829
// Version config
2930
vc := &cfg.Version
3031
fs.StringVar(&vc.Format, "format", "", wh("The version's format can be: 'short', 'json'"))
32+
fs.BoolVar(&vc.Debug, "debug", false, wh("Add build information"))
3133
}
3234

3335
func (e *Executor) initVersion() {
@@ -37,26 +39,45 @@ func (e *Executor) initVersion() {
3739
Args: cobra.NoArgs,
3840
ValidArgsFunction: cobra.NoFileCompletions,
3941
RunE: func(cmd *cobra.Command, _ []string) error {
42+
if e.cfg.Version.Debug {
43+
info, ok := debug.ReadBuildInfo()
44+
if !ok {
45+
return nil
46+
}
47+
48+
switch strings.ToLower(e.cfg.Version.Format) {
49+
case "json":
50+
return json.NewEncoder(os.Stdout).Encode(versionInfo{
51+
Info: e.buildInfo,
52+
BuildInfo: info,
53+
})
54+
55+
default:
56+
fmt.Println(info.String())
57+
return printVersion(os.Stdout, e.buildInfo)
58+
}
59+
}
60+
4061
switch strings.ToLower(e.cfg.Version.Format) {
4162
case "short":
42-
fmt.Println(e.version)
63+
fmt.Println(e.buildInfo.Version)
4364
return nil
4465

4566
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)
67+
return json.NewEncoder(os.Stdout).Encode(e.buildInfo)
5268

5369
default:
54-
fmt.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
55-
return nil
70+
return printVersion(os.Stdout, e.buildInfo)
5671
}
5772
},
5873
}
5974

6075
e.rootCmd.AddCommand(versionCmd)
6176
e.initVersionConfiguration(versionCmd)
6277
}
78+
79+
func printVersion(w io.Writer, buildInfo BuildInfo) error {
80+
_, err := fmt.Fprintf(w, "golangci-lint has version %s built with %s from %s on %s\n",
81+
buildInfo.Version, buildInfo.GoVersion, buildInfo.Commit, buildInfo.Date)
82+
return err
83+
}

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewDefault() *Config {
3838

3939
type Version struct {
4040
Format string `mapstructure:"format"`
41+
Debug bool `mapstructure:"debug"`
4142
}
4243

4344
func IsGreaterThanOrEqualGo118(v string) bool {

0 commit comments

Comments
 (0)