From 7955052b01215a40087d526948459ce0627d76cf Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Thu, 11 Feb 2021 13:40:01 +0800 Subject: [PATCH] Chore: adjust version output format --- Makefile | 46 ++++++++++++++++++++++++--------------------- constant/version.go | 8 +++++--- engine/version.go | 6 +++--- stats/server.go | 6 +++--- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index e2521af..23f7812 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,20 @@ -BINDIR = "bin" -NAME = "tun2socks" +BINARY = tun2socks +OUTPUT = bin -BUILD_FLAGS = "-v" -BUILD_TAGS = "" -BUILD_TIME = $(shell date -u '+%FT%TZ') -BUILD_VERSION = $(shell git describe --tags) +BUILD_FLAGS = -v +BUILD_TAGS = +BUILD_COMMIT = $(shell git describe --dirty --always) +BUILD_VERSION = $(shell git describe --abbrev=0 --tags HEAD) + +CGO_ENABLED = 0 +GO111MODULE = on LDFLAGS += -w -s -buildid= LDFLAGS += -X "github.com/xjasonlyu/tun2socks/constant.Version=$(BUILD_VERSION)" -LDFLAGS += -X "github.com/xjasonlyu/tun2socks/constant.BuildTime=$(BUILD_TIME)" # RFC3339 +LDFLAGS += -X "github.com/xjasonlyu/tun2socks/constant.GitCommit=$(BUILD_COMMIT)" -GO_BUILD = GO111MODULE=on CGO_ENABLED=0 go build $(BUILD_FLAGS) -ldflags '$(LDFLAGS)' -tags '$(BUILD_TAGS)' -trimpath +GO_BUILD = GO111MODULE=$(GO111MODULE) CGO_ENABLED=$(CGO_ENABLED) \ + go build $(BUILD_FLAGS) -ldflags '$(LDFLAGS)' -tags '$(BUILD_TAGS)' -trimpath PLATFORM_LIST = \ darwin-amd64 \ @@ -27,45 +31,45 @@ WINDOWS_ARCH_LIST = \ all: linux-amd64 darwin-amd64 windows-amd64 tun2socks: - $(GO_BUILD) -o $(BINDIR)/$(NAME) + $(GO_BUILD) -o $(OUTPUT)/$(BINARY) darwin-amd64: - GOARCH=amd64 GOOS=darwin $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=amd64 GOOS=darwin $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ freebsd-amd64: - GOARCH=amd64 GOOS=freebsd $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=amd64 GOOS=freebsd $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ freebsd-arm64: - GOARCH=arm64 GOOS=freebsd $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=arm64 GOOS=freebsd $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ linux-amd64: - GOARCH=amd64 GOOS=linux $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=amd64 GOOS=linux $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ linux-arm64: - GOARCH=arm64 GOOS=linux $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=arm64 GOOS=linux $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ openbsd-amd64: - GOARCH=amd64 GOOS=openbsd $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=amd64 GOOS=openbsd $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ openbsd-arm64: - GOARCH=arm64 GOOS=openbsd $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@ + GOARCH=arm64 GOOS=openbsd $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@ windows-amd64: - GOARCH=amd64 GOOS=windows $(GO_BUILD) -o $(BINDIR)/$(NAME)-$@.exe + GOARCH=amd64 GOOS=windows $(GO_BUILD) -o $(OUTPUT)/$(BINARY)-$@.exe gz_releases=$(addsuffix .gz, $(PLATFORM_LIST)) zip_releases=$(addsuffix .zip, $(WINDOWS_ARCH_LIST)) $(gz_releases): %.gz : % - chmod +x $(BINDIR)/$(NAME)-$(basename $@) - gzip -f -S .gz $(BINDIR)/$(NAME)-$(basename $@) + chmod +x $(OUTPUT)/$(BINARY)-$(basename $@) + gzip -f -S .gz $(OUTPUT)/$(BINARY)-$(basename $@) $(zip_releases): %.zip : % - zip -m -j $(BINDIR)/$(NAME)-$(basename $@).zip $(BINDIR)/$(NAME)-$(basename $@).exe + zip -m -j $(OUTPUT)/$(BINARY)-$(basename $@).zip $(OUTPUT)/$(BINARY)-$(basename $@).exe all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST) releases: $(gz_releases) $(zip_releases) clean: - rm $(BINDIR)/* + rm $(OUTPUT)/* diff --git a/constant/version.go b/constant/version.go index c34a6fd..cdaf0cf 100755 --- a/constant/version.go +++ b/constant/version.go @@ -1,8 +1,10 @@ package constant -const Name = "tun2socks" +const ( + Name = "tun2socks" +) var ( - Version = "" - BuildTime = "" + Version string + GitCommit string ) diff --git a/engine/version.go b/engine/version.go index 95f5a86..2c1681c 100644 --- a/engine/version.go +++ b/engine/version.go @@ -5,7 +5,7 @@ import ( "runtime" "strings" - "github.com/xjasonlyu/tun2socks/constant" + V "github.com/xjasonlyu/tun2socks/constant" ) func showVersion() { @@ -14,9 +14,9 @@ func showVersion() { } func versionString() string { - return fmt.Sprintf("%s %s\n", constant.Name, strings.TrimPrefix(constant.Version, "v")) + return fmt.Sprintf("%s-%s\n", V.Name, strings.TrimPrefix(V.Version, "v")) } func releaseString() string { - return fmt.Sprintf("%s/%s, %s, %s\n", runtime.GOOS, runtime.GOARCH, runtime.Version(), constant.BuildTime) + return fmt.Sprintf("%s/%s, %s, %s\n", runtime.GOOS, runtime.GOARCH, runtime.Version(), V.GitCommit) } diff --git a/stats/server.go b/stats/server.go index e0648ad..a22bffe 100755 --- a/stats/server.go +++ b/stats/server.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/xjasonlyu/tun2socks/constant" + V "github.com/xjasonlyu/tun2socks/constant" "github.com/xjasonlyu/tun2socks/log" "github.com/xjasonlyu/tun2socks/tunnel/statistic" @@ -60,7 +60,7 @@ func Start(addr, token string) error { } func hello(w http.ResponseWriter, r *http.Request) { - render.JSON(w, r, render.M{"hello": constant.Name}) + render.JSON(w, r, render.M{"hello": V.Name}) } func authenticator(token string) func(http.Handler) http.Handler { @@ -203,5 +203,5 @@ func traffic(w http.ResponseWriter, r *http.Request) { } func version(w http.ResponseWriter, r *http.Request) { - render.JSON(w, r, render.M{"version": constant.Version}) + render.JSON(w, r, render.M{"version": V.Version, "commit": V.GitCommit}) }