-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathversion.go
135 lines (117 loc) · 3.78 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package version
import (
"fmt"
"runtime"
"runtime/debug"
)
const (
unknownProperty = ""
)
// Compiler is a convenient alias for runtime.Compiler.
const Compiler = runtime.Compiler
// Version information
var (
// GoVersion is the version of the Go toolchain used to build the binary
// (e.g. "go1.19.2").
// It defaults to value of runtime.Version() if not explicitly overridden.
GoVersion = unknownProperty
// GitCommit is the commit hash of the Git repository's HEAD at
// build-time.
// It defaults to the value as collected by the runtime/debug package if
// not explicitly overridden.
GitCommit = unknownProperty
// GitCommitDate is GitCommit's commit date in RFC3339 format.
// It defaults to the value as collected by the runtime/debug package if
// not explicitly overridden.
GitCommitDate = unknownProperty
// GitTreeState becomes "dirty" if the source tree had local modifications
// at build-time.
// It stays empty otherwise and will not be shown in Print if this is the
// case.
GitTreeState = unknownProperty
// GitTag is meant to be injected with the tag name associated with
// GitCommit, by means of `go -ldflags` at build-time.
// It stays empty otherwise and will not be shown in Print if this is the
// case.
GitTag = unknownProperty
// BuildDate is meant to be injected with a string denoting the build time
// of the binary, by means of `go -ldflags` at build-time.
// It stays empty otherwise and will not be shown in Print if this is the
// case.
BuildDate = unknownProperty
// Platform is a string in the form of "GOOS/GOARCH", e.g. "linux/amd64".
Platform = unknownProperty
// BuildComments can be used to associate arbitrary extra information with
// the binary, by means of injection via `go -ldflags` at build-time.
BuildComments = unknownProperty
// Name is meant to be injected with the binary's intended name, by means
// of `go -ldflags` at build-time.
// It stays empty otherwise and will not be shown in Print if this is the
// case.
Name = unknownProperty
)
// This is for preventing access to the unpopulated properties.
func init() {
collectFromBuildInfo()
collectFromRuntime()
}
// Print prints out the collected version information.
func Print() {
xprintf := func(k string, v string) {
fmt.Printf("%s:\t%s\n", k, v)
}
if Name != unknownProperty {
xprintf("Name", Name)
}
xprintf("Go version", GoVersion)
xprintf("Git commit", GitCommit)
xprintf("Commit date", GitCommitDate)
if GitTreeState != unknownProperty {
xprintf("Git state", GitTreeState)
}
if BuildDate != unknownProperty {
xprintf("Build date", BuildDate)
}
if BuildComments != unknownProperty {
xprintf("Build comments", BuildComments)
}
xprintf("OS/Arch", Platform)
xprintf("Compiler", Compiler)
if GitTag != unknownProperty {
xprintf("Git tag", GitTag)
}
}
// collectFromBuildInfo tries to set the build information embedded in the running binary via Go module.
// It doesn't override data if were already set by Go -ldflags.
func collectFromBuildInfo() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
if GitCommit == unknownProperty && kv.Value != "" {
GitCommit = kv.Value
}
case "vcs.time":
if GitCommitDate == unknownProperty && kv.Value != "" {
GitCommitDate = kv.Value
}
case "vcs.modified":
if GitTreeState == unknownProperty && kv.Value == "true" {
GitTreeState = "dirty"
}
}
}
}
// collectFromRuntime tries to set the build information embedded in the running binary via go runtime.
// It doesn't override data if were already set by Go -ldflags.
func collectFromRuntime() {
if GoVersion == unknownProperty {
GoVersion = runtime.Version()
}
if Platform == unknownProperty {
Platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
}
}