@@ -20,14 +20,24 @@ var (
2020 vcsRevision = unknown
2121 vcsModified = unknown
2222
23- time = unknown
24- vcsTag = unknown
23+ buildTime = unknown
24+ version = unknown
2525)
2626
2727// Info represents the build information for a Go binary.
2828// It contains details about the Go environment used for building,
2929// version control system state, and custom build-time information.
3030type 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+
3141 //
3242 // Basic go information, detected automatically.
3343 //
@@ -48,25 +58,16 @@ type Info struct {
4858 // VCSModified is the version control system modified status. "true" in case
4959 // repo is "dirty".
5060 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"`
6061}
6162
6263// 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.
6465// 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.
6666func (i Info ) String () string {
6767 b := & strings.Builder {}
6868
69- b .WriteString ("built with " )
69+ b .WriteString (i .Version )
70+ b .WriteString (" built with " )
7071 b .WriteString (i .GoVersion )
7172 b .WriteString (" for " )
7273 b .WriteString (i .GoOS )
@@ -75,12 +76,6 @@ func (i Info) String() string {
7576 b .WriteString (" from " )
7677 b .WriteString (i .VCSRevision )
7778
78- if i .BuildTag != unknown {
79- b .WriteString (" [" )
80- b .WriteString (i .BuildTag )
81- b .WriteRune (']' )
82- }
83-
8479 if i .VCSModified == "true" {
8580 b .WriteString (" (dirty)" )
8681 }
@@ -96,19 +91,20 @@ func (i Info) String() string {
9691// build information as key-value pairs.
9792func (i Info ) ToFields () fields.List {
9893 return fields.List {
94+ fields .F ("version" , i .Version ),
9995 fields .F ("go-version" , i .GoVersion ),
10096 fields .F ("go-os" , i .GoOS ),
10197 fields .F ("go-arch" , i .GoArch ),
10298 fields .F ("vcs-revision" , i .VCSRevision ),
10399 fields .F ("vcs-modified" , i .VCSModified ),
104100 fields .F ("build-time" , i .BuildTime ),
105- fields .F ("build-tag" , i .BuildTag ),
106101 }
107102}
108103
109104// GetInfo returns the build information.
110105// It automatically parses build information from runtime debug data
111106// 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.
112108func GetInfo () Info {
113109 if ! buildInfoParsed {
114110 buildInfoParsed = true
@@ -117,22 +113,24 @@ func GetInfo() Info {
117113 }
118114
119115 return Info {
116+ Version : version ,
120117 GoVersion : goVersion ,
121118 GoOS : goOS ,
122119 GoArch : goArch ,
123120 VCSRevision : vcsRevision ,
124121 VCSModified : vcsModified ,
125- BuildTime : time ,
126- BuildTag : vcsTag ,
122+ BuildTime : buildTime ,
127123 }
128124}
129125
130126//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 ,
136134}
137135
138136func parseBuildInfo () {
@@ -143,9 +141,13 @@ func parseBuildInfo() {
143141
144142 goVersion = info .GoVersion
145143
144+ if version == unknown {
145+ version = info .Main .Version
146+ }
147+
146148 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
149151 }
150152 }
151153}
0 commit comments