Skip to content

Commit a5b6129

Browse files
committed
Improve version metadata
1 parent ae1468f commit a5b6129

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

info/version.go

+30-10
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,33 @@ import (
1111
)
1212

1313
var (
14-
name string
14+
name string
15+
license string
16+
1517
version = "dev build"
16-
buildSource = "[source unknown]"
17-
buildTime = "[build time unknown]"
18-
license = "[license unknown]"
18+
buildSource = "unknown"
19+
buildTime = "unknown"
1920

2021
info *Info
2122
loadInfo sync.Once
2223
)
2324

25+
func init() {
26+
// Convert version string space placeholders.
27+
version = strings.ReplaceAll(version, "_", " ")
28+
buildSource = strings.ReplaceAll(buildSource, "_", " ")
29+
buildTime = strings.ReplaceAll(buildTime, "_", " ")
30+
}
31+
2432
// Info holds the programs meta information.
25-
type Info struct {
33+
type Info struct { //nolint:maligned
2634
Name string
2735
Version string
2836
License string
2937

3038
Source string
3139
BuildTime string
40+
CGO bool
3241

3342
Commit string
3443
CommitTime string
@@ -56,23 +65,26 @@ func GetInfo() *Info {
5665
buildSettings[setting.Key] = setting.Value
5766
}
5867

68+
fmt.Println(buildSettings)
69+
5970
info = &Info{
6071
Name: name,
6172
Version: version,
6273
License: license,
6374
Source: buildSource,
6475
BuildTime: buildTime,
76+
CGO: buildSettings["CGO_ENABLED"] == "1",
6577
Commit: buildSettings["vcs.revision"],
6678
CommitTime: buildSettings["vcs.time"],
6779
Dirty: buildSettings["vcs.modified"] == "true",
6880
BuildInfo: *buildInfo,
6981
}
7082

7183
if info.Commit == "" {
72-
info.Commit = "[commit unknown]"
84+
info.Commit = "unknown"
7385
}
7486
if info.CommitTime == "" {
75-
info.CommitTime = "[commit time unknown]"
87+
info.CommitTime = "unknown"
7688
}
7789
})
7890

@@ -99,11 +111,19 @@ func FullVersion() string {
99111
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, Version()))
100112

101113
// Build info.
102-
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
114+
cgoInfo := "-cgo"
115+
if info.CGO {
116+
cgoInfo = "+cgo"
117+
}
118+
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s %s) for %s/%s\n", runtime.Version(), runtime.Compiler, cgoInfo, runtime.GOOS, runtime.GOARCH))
103119
builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime))
104120

105121
// Commit info.
106-
builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit))
122+
dirtyInfo := "clean"
123+
if info.Dirty {
124+
dirtyInfo = "dirty"
125+
}
126+
builder.WriteString(fmt.Sprintf("\ncommit %s (%s)\n", info.Commit, dirtyInfo))
107127
builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
108128
builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))
109129

@@ -121,7 +141,7 @@ func CheckVersion() error {
121141
return nil // testing on windows
122142
default:
123143
// check version information
124-
if name == "[NAME]" || license == "[license unknown]" {
144+
if name == "" || license == "" {
125145
return errors.New("must call SetInfo() before calling CheckVersion()")
126146
}
127147
}

0 commit comments

Comments
 (0)