Przeglądaj źródła

Use buildinfo to get a version

tags/v1.0.1
9seconds 6 lat temu
rodzic
commit
b2e7e1a225
2 zmienionych plików z 27 dodań i 7 usunięć
  1. 1
    4
      Makefile
  2. 26
    3
      main.go

+ 1
- 4
Makefile Wyświetl plik

@@ -6,10 +6,7 @@ CC_BINARIES  := $(shell bash -c "echo -n $(APP_NAME)-{linux,freebsd,openbsd}-{38
6 6
 
7 7
 GOLANGCI_LINT_VERSION := v1.21.0
8 8
 
9
-VERSION_GO         := $(shell go version)
10
-VERSION_DATE       := $(shell date -Ru)
11
-VERSION_TAG        := $(shell git describe --tags --always)
12
-COMMON_BUILD_FLAGS := -ldflags="-s -w -X 'main.version=$(VERSION_TAG) ($(VERSION_GO)) [$(VERSION_DATE)]'"
9
+COMMON_BUILD_FLAGS := -ldflags="-s -w"
13 10
 
14 11
 MOD_ON  := env GO111MODULE=on
15 12
 MOD_OFF := env GO111MODULE=auto

+ 26
- 3
main.go Wyświetl plik

@@ -3,6 +3,8 @@ package main
3 3
 import (
4 4
 	"math/rand"
5 5
 	"os"
6
+	"runtime/debug"
7
+	"strings"
6 8
 	"time"
7 9
 
8 10
 	kingpin "gopkg.in/alecthomas/kingpin.v2"
@@ -12,8 +14,6 @@ import (
12 14
 	"github.com/9seconds/mtg/utils"
13 15
 )
14 16
 
15
-var version = "dev" // this has to be set by build ld flags
16
-
17 17
 var (
18 18
 	app = kingpin.New("MTG", "Simple MTPROTO proxy.")
19 19
 
@@ -114,7 +114,7 @@ var (
114 114
 
115 115
 func main() {
116 116
 	rand.Seed(time.Now().UTC().UnixNano())
117
-	app.Version(version)
117
+	app.Version(getVersion())
118 118
 	app.HelpFlag.Short('h')
119 119
 
120 120
 	if err := utils.SetLimits(); err != nil {
@@ -153,3 +153,26 @@ func main() {
153 153
 		}
154 154
 	}
155 155
 }
156
+
157
+func getVersion() string {
158
+	if info, ok := debug.ReadBuildInfo(); ok {
159
+		builder := strings.Builder{}
160
+		version := info.Main.Version
161
+
162
+		if version == "(devel)" {
163
+			version = "dev"
164
+		}
165
+
166
+		builder.WriteString(version)
167
+
168
+		if info.Main.Sum != "" {
169
+			builder.WriteString(" (checksum: ")
170
+			builder.WriteString(info.Main.Sum)
171
+			builder.WriteRune(')')
172
+		}
173
+
174
+		return builder.String()
175
+	}
176
+
177
+	return "dev"
178
+}

Ładowanie…
Anuluj
Zapisz