Skip to content

Commit a90357b

Browse files
authored
Merge pull request #226 from safing/migrate-build-info
Migrate to runtime/debug.BuildInfo for most VCS information
2 parents 704e9e2 + 045eedc commit a90357b

File tree

2 files changed

+67
-69
lines changed

2 files changed

+67
-69
lines changed

info/version.go

+58-57
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,89 @@ import (
55
"fmt"
66
"os"
77
"runtime"
8+
"runtime/debug"
89
"strings"
10+
"sync"
911
)
1012

1113
var (
12-
name = "[NAME]"
13-
version = "[version unknown]"
14-
commit = "[commit unknown]"
15-
license = "[license unknown]"
16-
buildOptions = "[options unknown]"
17-
buildUser = "[user unknown]"
18-
buildHost = "[host unknown]"
19-
buildDate = "[date unknown]"
20-
buildSource = "[source unknown]"
21-
22-
compareVersion bool
14+
name string
15+
version = "dev build"
16+
buildSource = "[source unknown]"
17+
license = "[license unknown]"
18+
19+
info *Info
20+
loadInfo sync.Once
2321
)
2422

2523
// Info holds the programs meta information.
2624
type Info struct {
27-
Name string
28-
Version string
29-
License string
30-
Commit string
31-
BuildOptions string
32-
BuildUser string
33-
BuildHost string
34-
BuildDate string
35-
BuildSource string
25+
Name string
26+
Version string
27+
License string
28+
Commit string
29+
Time string
30+
Source string
31+
Dirty bool
32+
33+
debug.BuildInfo
3634
}
3735

3836
// Set sets meta information via the main routine. This should be the first thing your program calls.
3937
func Set(setName string, setVersion string, setLicenseName string, compareVersionToTag bool) {
4038
name = setName
4139
version = setVersion
4240
license = setLicenseName
43-
compareVersion = compareVersionToTag
4441
}
4542

4643
// GetInfo returns all the meta information about the program.
4744
func GetInfo() *Info {
48-
return &Info{
49-
Name: name,
50-
Version: version,
51-
Commit: commit,
52-
License: license,
53-
BuildOptions: buildOptions,
54-
BuildUser: buildUser,
55-
BuildHost: buildHost,
56-
BuildDate: buildDate,
57-
BuildSource: buildSource,
58-
}
45+
loadInfo.Do(func() {
46+
buildInfo, _ := debug.ReadBuildInfo()
47+
buildSettings := make(map[string]string)
48+
for _, setting := range buildInfo.Settings {
49+
buildSettings[setting.Key] = setting.Value
50+
}
51+
52+
info = &Info{
53+
Name: name,
54+
Version: version,
55+
License: license,
56+
BuildInfo: *buildInfo,
57+
Source: buildSource,
58+
Commit: buildSettings["vcs.revision"],
59+
Time: buildSettings["vcs.time"],
60+
Dirty: buildSettings["vcs.modified"] == "true",
61+
}
62+
})
63+
64+
return info
5965
}
6066

6167
// Version returns the short version string.
6268
func Version() string {
63-
if !compareVersion || strings.HasPrefix(commit, fmt.Sprintf("tags/v%s-0-", version)) {
64-
return version
69+
info := GetInfo()
70+
71+
if info.Dirty {
72+
return version + "*"
6573
}
66-
return version + "*"
74+
75+
return version
6776
}
6877

6978
// FullVersion returns the full and detailed version string.
7079
func FullVersion() string {
71-
s := ""
72-
if !compareVersion || strings.HasPrefix(commit, fmt.Sprintf("tags/v%s-0-", version)) {
73-
s += fmt.Sprintf("%s\nversion %s\n", name, version)
74-
} else {
75-
s += fmt.Sprintf("%s\ndevelopment build, built on top version %s\n", name, version)
76-
}
77-
s += fmt.Sprintf("\ncommit %s\n", commit)
78-
s += fmt.Sprintf("built with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)
79-
s += fmt.Sprintf(" using options %s\n", strings.ReplaceAll(buildOptions, "§", " "))
80-
s += fmt.Sprintf(" by %s@%s\n", buildUser, buildHost)
81-
s += fmt.Sprintf(" on %s\n", buildDate)
82-
s += fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, buildSource)
83-
return s
80+
info := GetInfo()
81+
82+
builder := new(strings.Builder)
83+
84+
builder.WriteString(fmt.Sprintf("%s\nversion %s\n", info.Name, Version()))
85+
builder.WriteString(fmt.Sprintf("\ncommit %s\n", info.Commit))
86+
builder.WriteString(fmt.Sprintf("built with %s (%s) %s/%s\n", runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH))
87+
builder.WriteString(fmt.Sprintf(" on %s\n", info.Time))
88+
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.\nThe source code is available here: %s", license, info.Source))
89+
90+
return builder.String()
8491
}
8592

8693
// CheckVersion checks if the metadata is ok.
@@ -92,17 +99,11 @@ func CheckVersion() error {
9299
return nil // testing on windows
93100
default:
94101
// check version information
95-
if name == "[NAME]" {
102+
if name == "[NAME]" || license == "[license unknown]" {
96103
return errors.New("must call SetInfo() before calling CheckVersion()")
97104
}
98-
if version == "[version unknown]" ||
99-
commit == "[commit unknown]" ||
100-
license == "[license unknown]" ||
101-
buildOptions == "[options unknown]" ||
102-
buildUser == "[user unknown]" ||
103-
buildHost == "[host unknown]" ||
104-
buildDate == "[date unknown]" ||
105-
buildSource == "[source unknown]" {
105+
106+
if version == "[version unknown]" {
106107
return errors.New("please build using the supplied build script.\n$ ./build {main.go|...}")
107108
}
108109
}

metrics/metrics_info.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ func registerInfoMetric() error {
1515
_, err := NewGauge(
1616
"info",
1717
map[string]string{
18-
"version": checkUnknown(meta.Version),
19-
"commit": checkUnknown(meta.Commit),
20-
"build_options": checkUnknown(meta.BuildOptions),
21-
"build_user": checkUnknown(meta.BuildUser),
22-
"build_host": checkUnknown(meta.BuildHost),
23-
"build_date": checkUnknown(meta.BuildDate),
24-
"build_source": checkUnknown(meta.BuildSource),
25-
"go_os": runtime.GOOS,
26-
"go_arch": runtime.GOARCH,
27-
"go_version": runtime.Version(),
28-
"go_compiler": runtime.Compiler,
29-
"comment": commentOption(),
18+
"version": checkUnknown(meta.Version),
19+
"commit": checkUnknown(meta.Commit),
20+
"build_date": checkUnknown(meta.Time),
21+
"build_source": checkUnknown(meta.Source),
22+
"go_os": runtime.GOOS,
23+
"go_arch": runtime.GOARCH,
24+
"go_version": runtime.Version(),
25+
"go_compiler": runtime.Compiler,
26+
"comment": commentOption(),
3027
},
3128
func() float64 {
3229
// Report as 0 the first time in order to detect (re)starts.

0 commit comments

Comments
 (0)