Преглед изворни кода

More comprehensive build info collection

tags/v2.1.7^2
9seconds пре 3 година
родитељ
комит
f9ad93dd06
2 измењених фајлова са 102 додато и 32 уклоњено
  1. 101
    0
      buildinfo.go
  2. 1
    32
      main.go

+ 101
- 0
buildinfo.go Прегледај датотеку

@@ -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
+}

+ 1
- 32
main.go Прегледај датотеку

@@ -9,10 +9,7 @@
9 9
 package main
10 10
 
11 11
 import (
12
-	"fmt"
13 12
 	"math/rand"
14
-	"runtime/debug"
15
-	"strconv"
16 13
 	"time"
17 14
 
18 15
 	"github.com/9seconds/mtg/v2/internal/cli"
@@ -20,8 +17,6 @@ import (
20 17
 	"github.com/alecthomas/kong"
21 18
 )
22 19
 
23
-var version = "dev" // has to be set by ldflags
24
-
25 20
 func main() {
26 21
 	rand.Seed(time.Now().UTC().UnixNano())
27 22
 
@@ -29,35 +24,9 @@ func main() {
29 24
 		panic(err)
30 25
 	}
31 26
 
32
-	if buildInfo, ok := debug.ReadBuildInfo(); ok {
33
-		vcsCommit := "<no-commit>"
34
-		vcsDate := time.Now()
35
-		vcsDirty := ""
36
-
37
-		for _, setting := range buildInfo.Settings {
38
-			switch setting.Key {
39
-			case "vcs.time":
40
-				vcsDate, _ = time.Parse(time.RFC3339, setting.Value)
41
-			case "vcs.revision":
42
-				vcsCommit = setting.Value
43
-			case "vcs.modified":
44
-				if isDirty, _ := strconv.ParseBool(setting.Value); isDirty {
45
-					vcsDirty = " [dirty]"
46
-				}
47
-			}
48
-		}
49
-
50
-		version = fmt.Sprintf("%s (%s: %s on %s%s)",
51
-			version,
52
-			buildInfo.GoVersion,
53
-			vcsDate.Format(time.RFC3339),
54
-			vcsCommit,
55
-			vcsDirty)
56
-	}
57
-
58 27
 	cli := &cli.CLI{}
59 28
 	ctx := kong.Parse(cli, kong.Vars{
60
-		"version": version,
29
+		"version": getVersion(),
61 30
 	})
62 31
 
63 32
 	ctx.FatalIfErrorf(ctx.Run(cli, version))

Loading…
Откажи
Сачувај