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

Use embedded buildinfo

tags/v2.1.6^2
9seconds пре 4 година
родитељ
комит
5df1f594cc
2 измењених фајлова са 31 додато и 4 уклоњено
  1. 2
    4
      Makefile
  2. 29
    0
      main.go

+ 2
- 4
Makefile Прегледај датотеку

4
 
4
 
5
 GOLANGCI_LINT_VERSION := v1.44.2
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
 FUZZ_FLAGS := -fuzztime=120s
10
 FUZZ_FLAGS := -fuzztime=120s
13
 
11
 

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

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

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