소스 검색

Align buildinfo with master

tags/v1.0.12
9seconds 3 년 전
부모
커밋
64859069ed
2개의 변경된 파일84개의 추가작업 그리고 26개의 파일을 삭제
  1. 84
    0
      buildinfo.go
  2. 0
    26
      main.go

+ 84
- 0
buildinfo.go 파일 보기

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

+ 0
- 26
main.go 파일 보기

3
 import (
3
 import (
4
 	"math/rand"
4
 	"math/rand"
5
 	"os"
5
 	"os"
6
-	"runtime/debug"
7
-	"strings"
8
 	"time"
6
 	"time"
9
 
7
 
10
 	"github.com/9seconds/mtg/cli"
8
 	"github.com/9seconds/mtg/cli"
12
 	kingpin "gopkg.in/alecthomas/kingpin.v2"
10
 	kingpin "gopkg.in/alecthomas/kingpin.v2"
13
 )
11
 )
14
 
12
 
15
-var version = "dev" // has to be set by ldflags
16
-
17
 var (
13
 var (
18
 	app = kingpin.New("mtg", "Simple MTPROTO proxy.")
14
 	app = kingpin.New("mtg", "Simple MTPROTO proxy.")
19
 
15
 
161
 		}
157
 		}
162
 	}
158
 	}
163
 }
159
 }
164
-
165
-func getVersion() string {
166
-	if version != "dev" {
167
-		return version
168
-	}
169
-
170
-	info, ok := debug.ReadBuildInfo()
171
-	if !ok {
172
-		return version
173
-	}
174
-
175
-	builder := strings.Builder{}
176
-	builder.WriteString(info.Main.Version)
177
-
178
-	if info.Main.Sum != "" {
179
-		builder.WriteString(" (checksum: ")
180
-		builder.WriteString(info.Main.Sum)
181
-		builder.WriteRune(')')
182
-	}
183
-
184
-	return builder.String()
185
-}

Loading…
취소
저장