|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+package main
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "crypto/sha256"
|
|
|
5
|
+ "encoding/base64"
|
|
|
6
|
+ "encoding/binary"
|
|
|
7
|
+ "fmt"
|
|
|
8
|
+ "io"
|
|
|
9
|
+ "runtime/debug"
|
|
|
10
|
+ "sort"
|
|
|
11
|
+ "strconv"
|
|
|
12
|
+ "time"
|
|
|
13
|
+)
|
|
|
14
|
+
|
|
|
15
|
+var version = "dev" // has to be set by ldflags
|
|
|
16
|
+
|
|
|
17
|
+const (
|
|
|
18
|
+ buildInfoModuleStart byte = iota
|
|
|
19
|
+ buildInfoModuleFinish
|
|
|
20
|
+ buildInfoModuleDelimeter
|
|
|
21
|
+)
|
|
|
22
|
+
|
|
|
23
|
+func getVersion() string {
|
|
|
24
|
+ goVersion, date, commit, modulesChecksum, dirty := getVersionData()
|
|
|
25
|
+
|
|
|
26
|
+ dirtySuffix := ""
|
|
|
27
|
+ if dirty {
|
|
|
28
|
+ dirtySuffix = " [dirty]"
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ return fmt.Sprintf("%s (%s: %s on %s%s, modules checksum %s)",
|
|
|
32
|
+ version,
|
|
|
33
|
+ goVersion,
|
|
|
34
|
+ date.Format(time.RFC3339),
|
|
|
35
|
+ commit,
|
|
|
36
|
+ dirtySuffix,
|
|
|
37
|
+ modulesChecksum)
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+func getVersionData() (goVersion string, date time.Time, commit string, modulesChecksum string, dirty bool) {
|
|
|
41
|
+ date = time.Now()
|
|
|
42
|
+
|
|
|
43
|
+ buildInfo, ok := debug.ReadBuildInfo()
|
|
|
44
|
+ if !ok {
|
|
|
45
|
+ return
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ goVersion = buildInfo.GoVersion
|
|
|
49
|
+
|
|
|
50
|
+ for _, setting := range buildInfo.Settings {
|
|
|
51
|
+ switch setting.Key {
|
|
|
52
|
+ case "vcs.time":
|
|
|
53
|
+ date, _ = time.Parse(time.RFC3339, setting.Value)
|
|
|
54
|
+ case "vcs.revision":
|
|
|
55
|
+ commit = setting.Value
|
|
|
56
|
+ case "vcs.modified":
|
|
|
57
|
+ dirty, _ = strconv.ParseBool(setting.Value)
|
|
|
58
|
+ }
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ hasher := sha256.New()
|
|
|
62
|
+ if _, err := io.WriteString(hasher, buildInfo.Path); err != nil {
|
|
|
63
|
+ panic(err)
|
|
|
64
|
+ }
|
|
|
65
|
+ binary.Write(hasher, binary.LittleEndian, uint64(1+len(buildInfo.Deps)))
|
|
|
66
|
+
|
|
|
67
|
+ sort.Slice(buildInfo.Deps, func(i, j int) bool {
|
|
|
68
|
+ return buildInfo.Deps[i].Path > buildInfo.Deps[j].Path
|
|
|
69
|
+ })
|
|
|
70
|
+
|
|
|
71
|
+ buildInfoCheckSumModule(hasher, &buildInfo.Main)
|
|
|
72
|
+ for _, module := range buildInfo.Deps {
|
|
|
73
|
+ buildInfoCheckSumModule(hasher, module)
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ modulesChecksum = base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
|
|
77
|
+
|
|
|
78
|
+ return
|
|
|
79
|
+}
|
|
|
80
|
+
|
|
|
81
|
+func buildInfoCheckSumModule(w io.Writer, module *debug.Module) {
|
|
|
82
|
+ w.Write([]byte{buildInfoModuleStart})
|
|
|
83
|
+
|
|
|
84
|
+ if _, err := io.WriteString(w, module.Path); err != nil {
|
|
|
85
|
+ panic(err)
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ w.Write([]byte{buildInfoModuleDelimeter})
|
|
|
89
|
+
|
|
|
90
|
+ if _, err := io.WriteString(w, module.Version); err != nil {
|
|
|
91
|
+ panic(err)
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ w.Write([]byte{buildInfoModuleDelimeter})
|
|
|
95
|
+
|
|
|
96
|
+ if _, err := io.WriteString(w, module.Sum); err != nil {
|
|
|
97
|
+ panic(err)
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ w.Write([]byte{buildInfoModuleFinish})
|
|
|
101
|
+}
|