-
Notifications
You must be signed in to change notification settings - Fork 93
/
version.go
94 lines (80 loc) · 1.74 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
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
// gitTag provides the git tag used to build this binary.
// This is set via ldflags at build time, which normally set by the release pipeline.
// For go install binary, this value stays empty.
var gitTag string
type Version struct {
Version string
GoVersion string
BuildTime string
Platform string
}
func loadVersion() Version {
rv := Version{
Version: "unknown",
GoVersion: "unknown",
BuildTime: "unknown",
Platform: runtime.GOOS + "/" + runtime.GOARCH,
}
if gitTag != "" {
rv.Version = gitTag
}
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return rv
}
rv.GoVersion = buildInfo.GoVersion
var (
modified bool
revision string
buildTime string
)
for _, s := range buildInfo.Settings {
if s.Value == "" {
continue
}
switch s.Key {
case "vcs.revision":
revision = s.Value
case "vcs.modified":
modified = s.Value == "true"
case "vcs.time":
buildTime = s.Value
}
}
// in Go install mode, this is a known issue that vcs information will not be available.
// ref: https://github.com/golang/go/issues/51279
// Fallback to use module version and stop here as vcs information is incomplete.
if revision == "" {
if buildInfo.Main.Version != "(devel)" {
// fallback to use module version (legacy usage)
rv.Version = buildInfo.Main.Version
}
return rv
}
if modified {
revision += "-dirty"
}
if gitTag != "" {
revision = gitTag + "/" + revision
}
rv.Version = revision
if buildTime != "" {
rv.BuildTime = buildTime
}
return rv
}
func (ver Version) String() string {
return fmt.Sprintf(
"\ngit hash: %s\nGo version: %s\nBuild time: %s\nPlatform: %s",
ver.Version,
ver.GoVersion,
ver.BuildTime,
ver.Platform,
)
}