-
-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathversion.go
169 lines (141 loc) Β· 3.95 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package info
import (
"errors"
"fmt"
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
)
var (
name string
license string
version = "dev build"
versionNumber = "0.0.0"
buildSource = "unknown"
buildTime = "unknown"
info *Info
loadInfo sync.Once
)
func init() {
// Replace space placeholders.
buildSource = strings.ReplaceAll(buildSource, "_", " ")
buildTime = strings.ReplaceAll(buildTime, "_", " ")
// Convert version string from git tag to expected format.
version = strings.TrimSpace(strings.ReplaceAll(strings.TrimPrefix(version, "v"), "_", " "))
versionNumber = strings.TrimSpace(strings.TrimSuffix(version, "dev build"))
if versionNumber == "" {
versionNumber = "0.0.0"
}
// Get build info.
buildInfo, _ := debug.ReadBuildInfo()
buildSettings := make(map[string]string)
for _, setting := range buildInfo.Settings {
buildSettings[setting.Key] = setting.Value
}
// Add "dev build" to version if repo is dirty.
if buildSettings["vcs.modified"] == "true" &&
!strings.HasSuffix(version, "dev build") {
version += " dev build"
}
}
// Info holds the programs meta information.
type Info struct { //nolint:maligned
Name string
Version string
VersionNumber string
License string
Source string
BuildTime string
CGO bool
Commit string
CommitTime string
Dirty bool
debug.BuildInfo
}
// Set sets meta information via the main routine. This should be the first thing your program calls.
func Set(setName string, setVersion string, setLicenseName string) {
name = setName
license = setLicenseName
if setVersion != "" {
version = setVersion
}
}
// GetInfo returns all the meta information about the program.
func GetInfo() *Info {
loadInfo.Do(func() {
buildInfo, _ := debug.ReadBuildInfo()
buildSettings := make(map[string]string)
for _, setting := range buildInfo.Settings {
buildSettings[setting.Key] = setting.Value
}
info = &Info{
Name: name,
Version: version,
VersionNumber: versionNumber,
License: license,
Source: buildSource,
BuildTime: buildTime,
CGO: buildSettings["CGO_ENABLED"] == "1",
Commit: buildSettings["vcs.revision"],
CommitTime: buildSettings["vcs.time"],
Dirty: buildSettings["vcs.modified"] == "true",
BuildInfo: *buildInfo,
}
if info.Commit == "" {
info.Commit = "unknown"
}
if info.CommitTime == "" {
info.CommitTime = "unknown"
}
})
return info
}
// Version returns the annotated version.
func Version() string {
return version
}
// VersionNumber returns the version number only.
func VersionNumber() string {
return versionNumber
}
// FullVersion returns the full and detailed version string.
func FullVersion() string {
info := GetInfo()
builder := new(strings.Builder)
// Name and version.
builder.WriteString(fmt.Sprintf("%s %s\n", info.Name, version))
// Build info.
cgoInfo := "-cgo"
if info.CGO {
cgoInfo = "+cgo"
}
builder.WriteString(fmt.Sprintf("\nbuilt with %s (%s %s) for %s/%s\n", runtime.Version(), runtime.Compiler, cgoInfo, runtime.GOOS, runtime.GOARCH))
builder.WriteString(fmt.Sprintf(" at %s\n", info.BuildTime))
// Commit info.
dirtyInfo := "clean"
if info.Dirty {
dirtyInfo = "dirty"
}
builder.WriteString(fmt.Sprintf("\ncommit %s (%s)\n", info.Commit, dirtyInfo))
builder.WriteString(fmt.Sprintf(" at %s\n", info.CommitTime))
builder.WriteString(fmt.Sprintf(" from %s\n", info.Source))
builder.WriteString(fmt.Sprintf("\nLicensed under the %s license.", license))
return builder.String()
}
// CheckVersion checks if the metadata is ok.
func CheckVersion() error {
switch {
case strings.HasSuffix(os.Args[0], ".test"):
return nil // testing on linux/darwin
case strings.HasSuffix(os.Args[0], ".test.exe"):
return nil // testing on windows
default:
// check version information
if name == "" || license == "" {
return errors.New("must call SetInfo() before calling CheckVersion()")
}
}
return nil
}