Skip to content

Commit e6bb308

Browse files
committed
[build] rename buildTag to version
As most commonly it will be used as version marker it is renamed to version. Change-Id: Ic9137e9540eb553ecf5e98b92a10fa0c4c220bea
1 parent bb328e0 commit e6bb308

File tree

3 files changed

+66
-47
lines changed

3 files changed

+66
-47
lines changed

build/doc.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@
33
//
44
// The package automatically extracts build information from Go's runtime debug data,
55
// including Go version, target OS and architecture, and version control system details.
6-
// It also provides methods to format this information as human-readable text or structured data.
6+
// It also provides methods to format this information as human-readable text or structured
7+
// data.
8+
//
9+
// The Info struct has the following fields:
10+
// - Version (user-defined, settable via ldflags or taken from debug info)
11+
// - BuildTime (user-defined, settable via ldflags)
12+
// - GoVersion
13+
// - GoOS
14+
// - GoArch
15+
// - VCSRevision
16+
// - VCSModified
17+
//
18+
// The ToFields() method returns fields in the same order as described.
19+
//
20+
// Any of following fields can be set via ldflags, and, if so, they won't be overridden
21+
// by parsed info, except for GoVersion.
722
//
823
// # Usage
924
//
@@ -28,9 +43,10 @@
2843
// # Custom Build Information
2944
//
3045
// Info can also include custom information that is expected to be set by developer at build time
31-
// via ldflags as follows:
46+
// via ldflags as follows (note the variables names):
3247
//
33-
// `go build -ldflags "-X 'dev.gaijin.team/go/golib/build.time=<current-time>' -X 'dev.gaijin.team/go/golib/build.vcsTag=v1.0.0'"``
48+
// go build -ldflags "-X 'dev.gaijin.team/go/golib/build.version=v1.0.0' -X 'dev.gaijin.team/go/golib/build.buildTime=<current-time>'"
3449
//
50+
// If not set, these fields will be populated from Go build info or default to "unknown".
3551
//nolint
3652
package build

build/info.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3030
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+
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.
6666
func (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.
9792
func (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.
112108
func 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

138136
func 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
}

build/info_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ func TestInfo_String(t *testing.T) { //nolint:tparallel
2626
VCSRevision: "abcdef123456",
2727
VCSModified: "false",
2828
BuildTime: "2023-07-15T12:34:56Z",
29-
BuildTag: "v1.0.0",
29+
Version: "v1.0.0",
3030
},
31-
expected: "built with go1.17.5 for linux amd64 from abcdef123456 [v1.0.0] build time 2023-07-15T12:34:56Z",
31+
expected: "v1.0.0 built with go1.17.5 for linux amd64 from abcdef123456 build time 2023-07-15T12:34:56Z",
3232
},
3333
{
34-
name: "with unknown build tag",
34+
name: "with unknown version",
3535
info: build.Info{
3636
GoVersion: "go1.18.0",
3737
GoOS: "darwin",
3838
GoArch: "arm64",
3939
VCSRevision: "987654abcdef",
4040
VCSModified: "unknown",
4141
BuildTime: "2023-08-20T10:11:12Z",
42-
BuildTag: "unknown",
42+
Version: "unknown",
4343
},
44-
expected: "built with go1.18.0 for darwin arm64 from 987654abcdef build time 2023-08-20T10:11:12Z",
44+
expected: "unknown built with go1.18.0 for darwin arm64 from 987654abcdef build time 2023-08-20T10:11:12Z",
4545
},
4646
{
4747
name: "with dirty repo",
@@ -52,23 +52,24 @@ func TestInfo_String(t *testing.T) { //nolint:tparallel
5252
VCSRevision: "1a2b3c4d5e6f",
5353
VCSModified: "true",
5454
BuildTime: "2023-09-25T15:16:17Z",
55-
BuildTag: "v2.0.0",
55+
Version: "v2.0.0",
5656
},
57-
//revive:disable-next-line:line-length-limit
58-
expected: "built with go1.19.0 for windows amd64 from 1a2b3c4d5e6f [v2.0.0] (dirty) build time 2023-09-25T15:16:17Z",
57+
//nolint:lll
58+
expected: "v2.0.0 built with go1.19.0 for windows amd64 from 1a2b3c4d5e6f (dirty) build time 2023-09-25T15:16:17Z",
5959
},
6060
{
61-
name: "unknown build tag and dirty repo",
61+
name: "unknown version and dirty repo",
6262
info: build.Info{
6363
GoVersion: "go1.20.0",
6464
GoOS: "freebsd",
6565
GoArch: "amd64",
6666
VCSRevision: "a1b2c3d4e5f6",
6767
VCSModified: "true",
6868
BuildTime: "2023-10-30T20:21:22Z",
69-
BuildTag: "unknown",
69+
Version: "unknown",
7070
},
71-
expected: "built with go1.20.0 for freebsd amd64 from a1b2c3d4e5f6 (dirty) build time 2023-10-30T20:21:22Z",
71+
//nolint:lll
72+
expected: "unknown built with go1.20.0 for freebsd amd64 from a1b2c3d4e5f6 (dirty) build time 2023-10-30T20:21:22Z",
7273
},
7374
}
7475

@@ -89,23 +90,23 @@ func TestInfo_ToFields(t *testing.T) {
8990
VCSRevision: "abcdef123456",
9091
VCSModified: "false",
9192
BuildTime: "2023-07-15T12:34:56Z",
92-
BuildTag: "v1.0.0",
93+
Version: "v1.0.0",
9394
}
9495

9596
expectedFields := fields.List{
97+
fields.F("version", "v1.0.0"),
9698
fields.F("go-version", "go1.17.5"),
9799
fields.F("go-os", "linux"),
98100
fields.F("go-arch", "amd64"),
99101
fields.F("vcs-revision", "abcdef123456"),
100102
fields.F("vcs-modified", "false"),
101103
fields.F("build-time", "2023-07-15T12:34:56Z"),
102-
fields.F("build-tag", "v1.0.0"),
103104
}
104105

105106
resultFields := info.ToFields()
106107

107108
// Test length
108-
assert.Equal(t, len(expectedFields), len(resultFields), "fields list length should match")
109+
assert.Len(t, resultFields, len(expectedFields), "fields list length should match")
109110

110111
// Test each field
111112
for i := range len(expectedFields) {

0 commit comments

Comments
 (0)