forked from Zxilly/go-size-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
100 lines (86 loc) · 2.13 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
package gsa
import (
"fmt"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/dustin/go-humanize"
)
const (
unknownVersion = "(devel)"
unknownProperty = "N/A"
)
const (
// StaticVersion when update this, also update the version string in workflow
StaticVersion = "1"
)
var (
version = unknownVersion
commit = unknownProperty
buildDate = unknownProperty
commitDate = unknownProperty
dirtyBuild = unknownProperty
)
func SprintVersion() string {
info, ok := debug.ReadBuildInfo()
if ok {
if version == unknownVersion && info.Main.Version != "" {
version = info.Main.Version
}
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
if commit == unknownProperty && kv.Value != "" {
commit = kv.Value
}
case "vcs.time":
if commitDate == unknownProperty && kv.Value != "" {
commitDate = kv.Value
}
case "vcs.modified":
if dirtyBuild == unknownProperty && kv.Value != "" {
dirtyBuild = kv.Value
}
}
}
}
formattedBool := func(b string) string {
switch b {
case "true":
return "yes"
case "false":
return "no"
default:
return b
}
}
const layout = "2006-01-02 15:04:05"
buildDateTime, err := time.Parse(time.RFC3339, buildDate)
if err == nil {
buildDate = buildDateTime.Format(layout) + " (" + humanize.Time(buildDateTime) + ")"
}
commitDateTime, err := time.Parse(time.RFC3339, commitDate)
if err == nil {
commitDate = commitDateTime.Format(layout) + " (" + humanize.Time(commitDateTime) + ")"
}
s := new(strings.Builder)
s.WriteString("▓▓▓ gsa\n\n")
values := map[string]string{
"Version": version,
"Git Commit": commit,
"Build Date": buildDate,
"Commit Date": commitDate,
"Dirty Build": formattedBool(dirtyBuild),
"Go Version": runtime.Version(),
"Platform": fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
keys := []string{"Version", "Git Commit", "Build Date", "Commit Date", "Dirty Build", "Go Version", "Platform"}
for _, k := range keys {
if values[k] == unknownProperty {
continue
}
s.WriteString(fmt.Sprintf(" %-11s %s\n", k, values[k]))
}
return s.String()
}