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
package buildstamp
import (
"runtime/debug"
"strconv"
)
var (
Kind string
Version string
SourceRoot string
Commit string
Dirty string
ChangedPaths string
BuiltUTC string
)
const KindDev = "dev"
type Info struct {
Kind string `json:"kind"`
Reproducible bool `json:"reproducible"`
SourceRoot string `json:"sourceRoot,omitempty"`
Commit string `json:"commit,omitempty"`
Dirty bool `json:"dirty"`
ChangedPaths int `json:"changedPaths"`
BuiltUTC string `json:"builtUtc,omitempty"`
}
func Dev() bool { return Kind == KindDev }
func Identity() (version, commit string) {
version, commit = Version, Commit
if bi, ok := debug.ReadBuildInfo(); ok {
if version == "" && bi.Main.Version != "" && bi.Main.Version != "(devel)" {
version = bi.Main.Version
}
for _, s := range bi.Settings {
if s.Key == "vcs.revision" && commit == "" {
commit = s.Value
}
}
}
return version, commit
}
func Read() *Info {
if !Dev() {
return nil
}
n, _ := strconv.Atoi(ChangedPaths)
return &Info{Kind: Kind, SourceRoot: SourceRoot, Commit: Commit, Dirty: Dirty == "true", ChangedPaths: n, BuiltUTC: BuiltUTC}
}