-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
69 lines (59 loc) · 1.46 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
// Package version provides the version that the binary was built at.
package version
import (
_ "embed"
"fmt"
"runtime/debug"
"strings"
)
//go:embed VERSION.txt
var version string
var (
// Long is a full version number for this build.
Long = ""
// Short is a short version number for this build.
Short = ""
// GitCommit is the git commit of the build.
GitCommit = ""
// GitDirty is the vcs.modified setting returned by debug.ReadBuildInfo.
GitDirty bool
)
func init() {
if Long != "" && Short != "" {
// Built in the recommended way, using build_dist.sh.
return
}
bi, ok := debug.ReadBuildInfo()
if !ok {
Long = strings.TrimSpace(version) + "-ERR-BuildInfo"
Short = Long
return
}
var dirty string // "-dirty" suffix if dirty
var commitHashAbbrev, commitDate string
for _, s := range bi.Settings {
switch s.Key {
case "vcs.revision":
GitCommit = s.Value
if len(s.Value) >= 9 {
commitHashAbbrev = s.Value[:9]
}
case "vcs.time":
if len(s.Value) >= len("yyyy-mm-dd") {
commitDate = s.Value[:len("yyyy-mm-dd")]
commitDate = strings.ReplaceAll(commitDate, "-", "")
}
case "vcs.modified":
if s.Value == "true" {
dirty = "-dirty"
GitDirty = true
}
}
}
// Backup path, using Go 1.18's built-in git stamping.
Short = strings.TrimSpace(version) + "-dev" + commitDate
Long = Short + "-t" + commitHashAbbrev + dirty
}
func Info(appName string) string {
return fmt.Sprintf("%s version %s", appName, Long)
}