-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildversion.go
56 lines (50 loc) · 954 Bytes
/
buildversion.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
package buildversion
import (
"fmt"
"runtime/debug"
"time"
)
type (
BuildVersion struct {
Time time.Time
Commit string
Branch string
Modified bool
VCS string
}
)
const (
timestampFormat = "20060102"
)
func Get() BuildVersion {
bv := BuildVersion{}
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.branch":
bv.Branch = setting.Value
case "vcs.time":
if t, e := time.Parse(time.RFC3339, setting.Value); e == nil {
bv.Time = t
} else {
panic(e)
}
case "vcs.revision":
bv.Commit = setting.Value
case "vcs.modified":
bv.Modified = setting.Value == "true"
case "vcs":
bv.VCS = setting.Value
}
}
}
if bv.Time.IsZero() {
bv.Time = time.Now()
bv.Commit = "DEV"
}
return bv
}
func String() string {
bv := Get()
return fmt.Sprintf("%s-%-0.7s", bv.Time.Format(timestampFormat), bv.Commit)
}