17
17
}
18
18
)
19
19
20
+ // isUnset returns true when the provided string should be treated as an
21
+ // "unset" value. Builds that inject ldflags such as "-X var=" will set the
22
+ // variable to the empty string, which previously prevented the runtime build
23
+ // information gathered via debug.ReadBuildInfo from populating the field. For
24
+ // the purposes of version reporting we treat both the empty string and the
25
+ // literal "unknown" as unset.
26
+ func isUnset (s string ) bool {
27
+ return s == "" || s == "unknown"
28
+ }
29
+
20
30
func String () string {
21
- return fmt .Sprintf ("version: %q, commit: %q, date: %q, state: %q" , version , gitCommit , commitDate , repoState )
31
+ return fmt .Sprintf ("version: %q, commit: %q, date: %q, state: %q" ,
32
+ valueOrUnknown (version ), valueOrUnknown (gitCommit ), valueOrUnknown (commitDate ), valueOrUnknown (repoState ))
33
+ }
34
+
35
+ func valueOrUnknown (v string ) string {
36
+ if v == "" {
37
+ return "unknown"
38
+ }
39
+ return v
22
40
}
23
41
24
42
func init () {
@@ -29,18 +47,20 @@ func init() {
29
47
for _ , setting := range info .Settings {
30
48
switch setting .Key {
31
49
case "vcs.revision" :
32
- if gitCommit == "unknown" {
50
+ if isUnset ( gitCommit ) {
33
51
gitCommit = setting .Value
34
52
}
35
53
case "vcs.time" :
36
- commitDate = setting .Value
54
+ if isUnset (commitDate ) {
55
+ commitDate = setting .Value
56
+ }
37
57
case "vcs.modified" :
38
58
if v , ok := stateMap [setting .Value ]; ok {
39
59
repoState = v
40
60
}
41
61
}
42
62
}
43
- if version == "unknown" {
63
+ if isUnset ( version ) {
44
64
version = info .Main .Version
45
65
}
46
66
}
0 commit comments