generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
version.go
76 lines (64 loc) · 1.38 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
package constants
import (
"runtime/debug"
"strings"
"text/template"
)
var (
// CLIVersion is the version of the application. Note that this is
// set at compile time using ldflags.
CLIVersion = "no-info"
// VerboseCLIVersion is the verbose version of the application.
// Note that this is set up at init time.
VerboseCLIVersion = ""
)
type versionStruct struct {
Version string
GoVersion string
Time string
Commit string
OS string
Arch string
Modified bool
}
const (
verboseTemplate = `Version: {{.Version}}
Go Version: {{.GoVersion}}
Git Commit: {{.Commit}}
Commit Date: {{.Time}}
OS/Arch: {{.OS}}/{{.Arch}}
Dirty: {{.Modified}}`
)
func init() {
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
var vvs versionStruct
vvs.Version = CLIVersion
vvs.GoVersion = bi.GoVersion
for _, kv := range bi.Settings {
switch kv.Key {
case "vcs.time":
vvs.Time = kv.Value
case "vcs.revision":
vvs.Commit = kv.Value
case "vcs.modified":
vvs.Modified = kv.Value == "true"
case "GOOS":
vvs.OS = kv.Value
case "GOARCH":
vvs.Arch = kv.Value
}
}
VerboseCLIVersion = vvs.String()
}
func (vvs *versionStruct) String() string {
stringBuilder := &strings.Builder{}
tmpl := template.Must(template.New("version").Parse(verboseTemplate))
err := tmpl.Execute(stringBuilder, vvs)
if err != nil {
panic(err)
}
return stringBuilder.String()
}