@@ -11,24 +11,33 @@ import (
11
11
)
12
12
13
13
var (
14
- name string
14
+ name string
15
+ license string
16
+
15
17
version = "dev build"
16
- buildSource = "[source unknown]"
17
- buildTime = "[build time unknown]"
18
- license = "[license unknown]"
18
+ buildSource = "unknown"
19
+ buildTime = "unknown"
19
20
20
21
info * Info
21
22
loadInfo sync.Once
22
23
)
23
24
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
+
24
32
// Info holds the programs meta information.
25
- type Info struct {
33
+ type Info struct { //nolint:maligned
26
34
Name string
27
35
Version string
28
36
License string
29
37
30
38
Source string
31
39
BuildTime string
40
+ CGO bool
32
41
33
42
Commit string
34
43
CommitTime string
@@ -56,23 +65,26 @@ func GetInfo() *Info {
56
65
buildSettings [setting .Key ] = setting .Value
57
66
}
58
67
68
+ fmt .Println (buildSettings )
69
+
59
70
info = & Info {
60
71
Name : name ,
61
72
Version : version ,
62
73
License : license ,
63
74
Source : buildSource ,
64
75
BuildTime : buildTime ,
76
+ CGO : buildSettings ["CGO_ENABLED" ] == "1" ,
65
77
Commit : buildSettings ["vcs.revision" ],
66
78
CommitTime : buildSettings ["vcs.time" ],
67
79
Dirty : buildSettings ["vcs.modified" ] == "true" ,
68
80
BuildInfo : * buildInfo ,
69
81
}
70
82
71
83
if info .Commit == "" {
72
- info .Commit = "[commit unknown] "
84
+ info .Commit = "unknown"
73
85
}
74
86
if info .CommitTime == "" {
75
- info .CommitTime = "[commit time unknown] "
87
+ info .CommitTime = "unknown"
76
88
}
77
89
})
78
90
@@ -99,11 +111,19 @@ func FullVersion() string {
99
111
builder .WriteString (fmt .Sprintf ("%s %s\n " , info .Name , Version ()))
100
112
101
113
// Build info.
102
- builder .WriteString (fmt .Sprintf ("\n built 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 ("\n built with %s (%s %s) for %s/%s\n " , runtime .Version (), runtime .Compiler , cgoInfo , runtime .GOOS , runtime .GOARCH ))
103
119
builder .WriteString (fmt .Sprintf (" at %s\n " , info .BuildTime ))
104
120
105
121
// Commit info.
106
- builder .WriteString (fmt .Sprintf ("\n commit %s\n " , info .Commit ))
122
+ dirtyInfo := "clean"
123
+ if info .Dirty {
124
+ dirtyInfo = "dirty"
125
+ }
126
+ builder .WriteString (fmt .Sprintf ("\n commit %s (%s)\n " , info .Commit , dirtyInfo ))
107
127
builder .WriteString (fmt .Sprintf (" at %s\n " , info .CommitTime ))
108
128
builder .WriteString (fmt .Sprintf (" from %s\n " , info .Source ))
109
129
@@ -121,7 +141,7 @@ func CheckVersion() error {
121
141
return nil // testing on windows
122
142
default :
123
143
// check version information
124
- if name == "[NAME] " || license == "[license unknown] " {
144
+ if name == "" || license == "" {
125
145
return errors .New ("must call SetInfo() before calling CheckVersion()" )
126
146
}
127
147
}
0 commit comments