@@ -20,14 +20,24 @@ var (
20
20
vcsRevision = unknown
21
21
vcsModified = unknown
22
22
23
- time = unknown
24
- vcsTag = unknown
23
+ buildTime = unknown
24
+ version = unknown
25
25
)
26
26
27
27
// Info represents the build information for a Go binary.
28
28
// It contains details about the Go environment used for building,
29
29
// version control system state, and custom build-time information.
30
30
type Info struct {
31
+ //
32
+ // User-defined information, set at build time.
33
+ //
34
+
35
+ // Version is the version of the binary. In case parameter is not set,
36
+ // via linker flags it will be taken from debug.ReadBuildInfo().
37
+ Version string `json:"version"`
38
+ // BuildTime is the build time.
39
+ BuildTime string `json:"build-time"`
40
+
31
41
//
32
42
// Basic go information, detected automatically.
33
43
//
@@ -48,25 +58,16 @@ type Info struct {
48
58
// VCSModified is the version control system modified status. "true" in case
49
59
// repo is "dirty".
50
60
VCSModified string `json:"vcs-modified"`
51
-
52
- //
53
- // User-defined information, set at build time.
54
- //
55
-
56
- // BuildTime is the build time.
57
- BuildTime string `json:"build-time"`
58
- // BuildTag is the vcs tag of the commit, binary built from.
59
- BuildTag string `json:"build-tag"`
60
61
}
61
62
62
63
// String returns a human-readable representation of build information.
63
- // The format includes Go version, OS, architecture, VCS revision, and build details.
64
+ // The format includes version, Go version, OS, architecture, VCS revision, and build details.
64
65
// If the repository was modified (dirty), this is indicated in the output.
65
- // If a build tag is present, it's included in square brackets. after the revision.
66
66
func (i Info ) String () string {
67
67
b := & strings.Builder {}
68
68
69
- b .WriteString ("built with " )
69
+ b .WriteString (i .Version )
70
+ b .WriteString (" built with " )
70
71
b .WriteString (i .GoVersion )
71
72
b .WriteString (" for " )
72
73
b .WriteString (i .GoOS )
@@ -75,12 +76,6 @@ func (i Info) String() string {
75
76
b .WriteString (" from " )
76
77
b .WriteString (i .VCSRevision )
77
78
78
- if i .BuildTag != unknown {
79
- b .WriteString (" [" )
80
- b .WriteString (i .BuildTag )
81
- b .WriteRune (']' )
82
- }
83
-
84
79
if i .VCSModified == "true" {
85
80
b .WriteString (" (dirty)" )
86
81
}
@@ -96,19 +91,20 @@ func (i Info) String() string {
96
91
// build information as key-value pairs.
97
92
func (i Info ) ToFields () fields.List {
98
93
return fields.List {
94
+ fields .F ("version" , i .Version ),
99
95
fields .F ("go-version" , i .GoVersion ),
100
96
fields .F ("go-os" , i .GoOS ),
101
97
fields .F ("go-arch" , i .GoArch ),
102
98
fields .F ("vcs-revision" , i .VCSRevision ),
103
99
fields .F ("vcs-modified" , i .VCSModified ),
104
100
fields .F ("build-time" , i .BuildTime ),
105
- fields .F ("build-tag" , i .BuildTag ),
106
101
}
107
102
}
108
103
109
104
// GetInfo returns the build information.
110
105
// It automatically parses build information from runtime debug data
111
106
// the first time it's called, and caches the result for subsequent calls.
107
+ // Values set via linker flags (-X) are preserved and not overwritten.
112
108
func GetInfo () Info {
113
109
if ! buildInfoParsed {
114
110
buildInfoParsed = true
@@ -117,22 +113,24 @@ func GetInfo() Info {
117
113
}
118
114
119
115
return Info {
116
+ Version : version ,
120
117
GoVersion : goVersion ,
121
118
GoOS : goOS ,
122
119
GoArch : goArch ,
123
120
VCSRevision : vcsRevision ,
124
121
VCSModified : vcsModified ,
125
- BuildTime : time ,
126
- BuildTag : vcsTag ,
122
+ BuildTime : buildTime ,
127
123
}
128
124
}
129
125
130
126
//nolint:gochecknoglobals
131
- var settingsProcessors = map [string ]func (s debug.BuildSetting ){
132
- "GOOS" : func (s debug.BuildSetting ) { goOS = s .Value },
133
- "GOARCH" : func (s debug.BuildSetting ) { goArch = s .Value },
134
- "vcs.revision" : func (s debug.BuildSetting ) { vcsRevision = s .Value },
135
- "vcs.modified" : func (s debug.BuildSetting ) { vcsModified = s .Value },
127
+ var settingsValues = map [string ]* string {
128
+ "GOOS" : & goOS ,
129
+ "GOARCH" : & goArch ,
130
+ "vcs.revision" : & vcsRevision ,
131
+ "vcs.modified" : & vcsModified ,
132
+ "build-time" : & buildTime ,
133
+ "version" : & version ,
136
134
}
137
135
138
136
func parseBuildInfo () {
@@ -143,9 +141,13 @@ func parseBuildInfo() {
143
141
144
142
goVersion = info .GoVersion
145
143
144
+ if version == unknown {
145
+ version = info .Main .Version
146
+ }
147
+
146
148
for _ , s := range info .Settings {
147
- if f , ok := settingsProcessors [s .Key ]; ok {
148
- f ( s )
149
+ if ptr , ok := settingsValues [s .Key ]; ok && * ptr == unknown {
150
+ * ptr = s . Value
149
151
}
150
152
}
151
153
}
0 commit comments