Skip to content

Commit d18acc5

Browse files
authored
dev: improve version output (#4483)
1 parent 85e1dee commit d18acc5

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

cmd/golangci-lint/main.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,64 @@ var (
1313
goVersion = "unknown"
1414

1515
// Populated by goreleaser during build
16-
version = "master"
16+
version = "unknown"
1717
commit = "?"
1818
date = ""
1919
)
2020

2121
func main() {
22-
if buildInfo, available := debug.ReadBuildInfo(); available {
23-
goVersion = buildInfo.GoVersion
22+
info := createBuildInfo()
2423

25-
if date == "" {
26-
version = buildInfo.Main.Version
27-
commit = fmt.Sprintf("(unknown, mod sum: %q)", buildInfo.Main.Sum)
28-
date = "(unknown)"
29-
}
24+
if err := commands.Execute(info); err != nil {
25+
_, _ = fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err)
26+
os.Exit(exitcodes.Failure)
3027
}
28+
}
3129

30+
func createBuildInfo() commands.BuildInfo {
3231
info := commands.BuildInfo{
33-
GoVersion: goVersion,
34-
Version: version,
3532
Commit: commit,
33+
Version: version,
34+
GoVersion: goVersion,
3635
Date: date,
3736
}
3837

39-
if err := commands.Execute(info); err != nil {
40-
_, _ = fmt.Fprintf(os.Stderr, "failed executing command with error %v\n", err)
41-
os.Exit(exitcodes.Failure)
38+
if buildInfo, available := debug.ReadBuildInfo(); available {
39+
info.GoVersion = buildInfo.GoVersion
40+
41+
if date == "" {
42+
info.Version = buildInfo.Main.Version
43+
44+
var revision string
45+
var modified string
46+
for _, setting := range buildInfo.Settings {
47+
// The `vcs.xxx` information is only available with `go build`.
48+
// This information is are not available with `go install` or `go run`.
49+
switch setting.Key {
50+
case "vcs.time":
51+
info.Date = setting.Value
52+
case "vcs.revision":
53+
revision = setting.Value
54+
case "vcs.modified":
55+
modified = setting.Value
56+
}
57+
}
58+
59+
if revision == "" {
60+
revision = "unknown"
61+
}
62+
63+
if modified == "" {
64+
modified = "?"
65+
}
66+
67+
if info.Date == "" {
68+
info.Date = "(unknown)"
69+
}
70+
71+
info.Commit = fmt.Sprintf("(%s, modified: %s, mod sum: %q)", revision, modified, buildInfo.Main.Sum)
72+
}
4273
}
74+
75+
return info
4376
}

0 commit comments

Comments
 (0)