-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildinfo.go
75 lines (71 loc) · 1.33 KB
/
buildinfo.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
package governor
import (
"runtime/debug"
"strings"
"time"
)
type (
VCSBuildInfo struct {
GoVersion string
ModVersion string
VCS string
VCSRevision string
VCSTime time.Time
VCSModified bool
}
)
// ReadVCSBuildInfo reads vcs build info from [runtime/debug.ReadBuildInfo]
func ReadVCSBuildInfo() VCSBuildInfo {
v := VCSBuildInfo{
GoVersion: "",
ModVersion: "(devel)",
VCS: "",
VCSRevision: "",
VCSTime: time.Time{},
VCSModified: true,
}
if info, ok := debug.ReadBuildInfo(); ok {
v.GoVersion = info.GoVersion
v.ModVersion = info.Main.Version
for _, i := range info.Settings {
switch i.Key {
case "vcs":
v.VCS = i.Value
case "vcs.revision":
v.VCSRevision = i.Value
case "vcs.time":
t, err := time.Parse(time.RFC3339, i.Value)
if err == nil {
v.VCSTime = t
}
case "vcs.modified":
v.VCSModified = i.Value != "false"
}
}
}
return v
}
// VCSStr formats vcs info
func (v VCSBuildInfo) VCSStr() string {
b := strings.Builder{}
hasVCS := false
if v.VCS != "" {
hasVCS = true
b.WriteString(v.VCS)
}
if v.VCSRevision != "" {
if hasVCS {
b.WriteString("-")
}
hasVCS = true
b.WriteString(v.VCSRevision)
}
if v.VCSModified {
if hasVCS {
b.WriteString("-")
}
hasVCS = true
b.WriteString("dev")
}
return b.String()
}