|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+ buildInfo, ok := debug.ReadBuildInfo()
|
|
|
25
|
+ if !ok {
|
|
|
26
|
+ return version
|
|
|
27
|
+ }
|
|
|
28
|
+
|
|
|
29
|
+ date := time.Now()
|
|
|
30
|
+ commit := ""
|
|
|
31
|
+ goVersion := buildInfo.GoVersion
|
|
|
32
|
+ dirtySuffix := ""
|
|
|
33
|
+
|
|
|
34
|
+ for _, setting := range buildInfo.Settings {
|
|
|
35
|
+ switch setting.Key {
|
|
|
36
|
+ case "vcs.time":
|
|
|
37
|
+ date, _ = time.Parse(time.RFC3339, setting.Value)
|
|
|
38
|
+ case "vcs.revision":
|
|
|
39
|
+ commit = setting.Value
|
|
|
40
|
+ case "vcs.modified":
|
|
|
41
|
+ if dirty, _ := strconv.ParseBool(setting.Value); dirty {
|
|
|
42
|
+ dirtySuffix = " [dirty]"
|
|
|
43
|
+ }
|
|
|
44
|
+ }
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ hasher := sha256.New()
|
|
|
48
|
+
|
|
|
49
|
+ checksumModule := func(mod *debug.Module) {
|
|
|
50
|
+ hasher.Write([]byte{buildInfoModuleStart})
|
|
|
51
|
+
|
|
|
52
|
+ io.WriteString(hasher, mod.Path) //nolint: errcheck
|
|
|
53
|
+ hasher.Write([]byte{buildInfoModuleDelimeter})
|
|
|
54
|
+
|
|
|
55
|
+ io.WriteString(hasher, mod.Version) //nolint: errcheck
|
|
|
56
|
+ hasher.Write([]byte{buildInfoModuleDelimeter})
|
|
|
57
|
+
|
|
|
58
|
+ io.WriteString(hasher, mod.Sum) //nolint: errcheck
|
|
|
59
|
+
|
|
|
60
|
+ hasher.Write([]byte{buildInfoModuleFinish})
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ io.WriteString(hasher, buildInfo.Path) //nolint: errcheck
|
|
|
64
|
+
|
|
|
65
|
+ binary.Write(hasher, binary.LittleEndian, uint64(1+len(buildInfo.Deps))) //nolint: errcheck
|
|
|
66
|
+
|
|
|
67
|
+ sort.Slice(buildInfo.Deps, func(i, j int) bool {
|
|
|
68
|
+ return buildInfo.Deps[i].Path > buildInfo.Deps[j].Path
|
|
|
69
|
+ })
|
|
|
70
|
+
|
|
|
71
|
+ checksumModule(&buildInfo.Main)
|
|
|
72
|
+
|
|
|
73
|
+ for _, module := range buildInfo.Deps {
|
|
|
74
|
+ checksumModule(module)
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ return fmt.Sprintf("%s (%s: %s on %s%s, modules checksum %s)",
|
|
|
78
|
+ version,
|
|
|
79
|
+ goVersion,
|
|
|
80
|
+ date.Format(time.RFC3339),
|
|
|
81
|
+ commit,
|
|
|
82
|
+ dirtySuffix,
|
|
|
83
|
+ base64.StdEncoding.EncodeToString(hasher.Sum(nil)))
|
|
|
84
|
+}
|