From b2e7e1a2259252da2af0f4c134b9371eb49db212 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Tue, 19 Nov 2019 11:36:14 +0300 Subject: [PATCH] Use buildinfo to get a version --- Makefile | 5 +---- main.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index e56ab75..c2f0815 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,7 @@ CC_BINARIES := $(shell bash -c "echo -n $(APP_NAME)-{linux,freebsd,openbsd}-{38 GOLANGCI_LINT_VERSION := v1.21.0 -VERSION_GO := $(shell go version) -VERSION_DATE := $(shell date -Ru) -VERSION_TAG := $(shell git describe --tags --always) -COMMON_BUILD_FLAGS := -ldflags="-s -w -X 'main.version=$(VERSION_TAG) ($(VERSION_GO)) [$(VERSION_DATE)]'" +COMMON_BUILD_FLAGS := -ldflags="-s -w" MOD_ON := env GO111MODULE=on MOD_OFF := env GO111MODULE=auto diff --git a/main.go b/main.go index 65c0ded..c06b415 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "math/rand" "os" + "runtime/debug" + "strings" "time" kingpin "gopkg.in/alecthomas/kingpin.v2" @@ -12,8 +14,6 @@ import ( "github.com/9seconds/mtg/utils" ) -var version = "dev" // this has to be set by build ld flags - var ( app = kingpin.New("MTG", "Simple MTPROTO proxy.") @@ -114,7 +114,7 @@ var ( func main() { rand.Seed(time.Now().UTC().UnixNano()) - app.Version(version) + app.Version(getVersion()) app.HelpFlag.Short('h') if err := utils.SetLimits(); err != nil { @@ -153,3 +153,26 @@ func main() { } } } + +func getVersion() string { + if info, ok := debug.ReadBuildInfo(); ok { + builder := strings.Builder{} + version := info.Main.Version + + if version == "(devel)" { + version = "dev" + } + + builder.WriteString(version) + + if info.Main.Sum != "" { + builder.WriteString(" (checksum: ") + builder.WriteString(info.Main.Sum) + builder.WriteRune(')') + } + + return builder.String() + } + + return "dev" +}