Bläddra i källkod

Use embedded buildinfo

tags/v2.1.6^2
9seconds 4 år sedan
förälder
incheckning
5df1f594cc
2 ändrade filer med 31 tillägg och 4 borttagningar
  1. 2
    4
      Makefile
  2. 29
    0
      main.go

+ 2
- 4
Makefile Visa fil

@@ -4,10 +4,8 @@ APP_NAME     := $(IMAGE_NAME)
4 4
 
5 5
 GOLANGCI_LINT_VERSION := v1.44.2
6 6
 
7
-VERSION_GO         := $(shell go version)
8
-VERSION_DATE       := $(shell date -Ru)
9
-VERSION_TAG        := $(shell git describe --tags --always)
10
-COMMON_BUILD_FLAGS := -trimpath -mod=readonly -ldflags="-extldflags '-static' -s -w -X 'main.version=$(VERSION_TAG) ($(VERSION_GO)) [$(VERSION_DATE)]'"
7
+VERSION            := $(shell git describe --exact-match HEAD 2>/dev/null || git describe --tags --always)
8
+COMMON_BUILD_FLAGS := -trimpath -mod=readonly -ldflags="-extldflags '-static' -s -w -X 'main.version=$(VERSION)'"
11 9
 
12 10
 FUZZ_FLAGS := -fuzztime=120s
13 11
 

+ 29
- 0
main.go Visa fil

@@ -9,7 +9,10 @@
9 9
 package main
10 10
 
11 11
 import (
12
+	"fmt"
12 13
 	"math/rand"
14
+	"runtime/debug"
15
+	"strconv"
13 16
 	"time"
14 17
 
15 18
 	"github.com/9seconds/mtg/v2/internal/cli"
@@ -26,6 +29,32 @@ func main() {
26 29
 		panic(err)
27 30
 	}
28 31
 
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
+
29 58
 	cli := &cli.CLI{}
30 59
 	ctx := kong.Parse(cli, kong.Vars{
31 60
 		"version": version,

Laddar…
Avbryt
Spara