From 830c231c43745b71806431da68fc522e9b307233 Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 31 Jan 2022 16:00:17 +0800 Subject: [PATCH] Refactor: standalone version module --- Makefile | 4 ++-- constant/version.go | 10 ---------- engine/engine.go | 5 ++++- engine/version.go | 22 ---------------------- stats/server.go | 2 +- version/version.go | 27 +++++++++++++++++++++++++++ 6 files changed, 34 insertions(+), 36 deletions(-) delete mode 100755 constant/version.go delete mode 100644 engine/version.go create mode 100755 version/version.go diff --git a/Makefile b/Makefile index 67dd407..53daef4 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,8 @@ CGO_ENABLED := 0 GO111MODULE := on LDFLAGS += -w -s -buildid= -LDFLAGS += -X "$(MODULE)/constant.Version=$(BUILD_VERSION)" -LDFLAGS += -X "$(MODULE)/constant.GitCommit=$(BUILD_COMMIT)" +LDFLAGS += -X "$(MODULE)/version.Version=$(BUILD_VERSION)" +LDFLAGS += -X "$(MODULE)/version.GitCommit=$(BUILD_COMMIT)" GO_BUILD = GO111MODULE=$(GO111MODULE) CGO_ENABLED=$(CGO_ENABLED) \ go build $(BUILD_FLAGS) -ldflags '$(LDFLAGS)' -tags '$(BUILD_TAGS)' -trimpath diff --git a/constant/version.go b/constant/version.go deleted file mode 100755 index cdaf0cf..0000000 --- a/constant/version.go +++ /dev/null @@ -1,10 +0,0 @@ -package constant - -const ( - Name = "tun2socks" -) - -var ( - Version string - GitCommit string -) diff --git a/engine/engine.go b/engine/engine.go index 9852ca8..237a9ea 100755 --- a/engine/engine.go +++ b/engine/engine.go @@ -2,6 +2,7 @@ package engine import ( "errors" + "fmt" "os" "github.com/xjasonlyu/tun2socks/v2/component/dialer" @@ -11,6 +12,7 @@ import ( "github.com/xjasonlyu/tun2socks/v2/proxy" "github.com/xjasonlyu/tun2socks/v2/stats" "github.com/xjasonlyu/tun2socks/v2/tunnel" + "github.com/xjasonlyu/tun2socks/v2/version" "gopkg.in/yaml.v3" ) @@ -60,7 +62,8 @@ func (e *engine) start() error { } if e.Version { - showVersion() + fmt.Println(version.String()) + fmt.Println(version.BuildString()) os.Exit(0) } diff --git a/engine/version.go b/engine/version.go deleted file mode 100644 index 0e32b25..0000000 --- a/engine/version.go +++ /dev/null @@ -1,22 +0,0 @@ -package engine - -import ( - "fmt" - "runtime" - "strings" - - V "github.com/xjasonlyu/tun2socks/v2/constant" -) - -func showVersion() { - fmt.Print(versionString()) - fmt.Print(releaseString()) -} - -func versionString() string { - 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(), V.GitCommit) -} diff --git a/stats/server.go b/stats/server.go index 75ced90..d798248 100755 --- a/stats/server.go +++ b/stats/server.go @@ -8,9 +8,9 @@ import ( "strings" "time" - V "github.com/xjasonlyu/tun2socks/v2/constant" "github.com/xjasonlyu/tun2socks/v2/log" "github.com/xjasonlyu/tun2socks/v2/tunnel/statistic" + V "github.com/xjasonlyu/tun2socks/v2/version" "github.com/go-chi/chi/v5" "github.com/go-chi/cors" diff --git a/version/version.go b/version/version.go new file mode 100755 index 0000000..d5951f2 --- /dev/null +++ b/version/version.go @@ -0,0 +1,27 @@ +package version + +import ( + "fmt" + "runtime" + "strings" +) + +const Name = "tun2socks" + +var ( + // Version can be set at link time by executing + // the command: `git describe --abbrev=0 --tags HEAD` + Version string + + // GitCommit can be set at link time by executing + // the command: `git rev-parse --short HEAD` + GitCommit string +) + +func String() string { + return fmt.Sprintf("%s-%s", Name, strings.TrimPrefix(Version, "v")) +} + +func BuildString() string { + return fmt.Sprintf("%s/%s, %s, %s", runtime.GOOS, runtime.GOARCH, runtime.Version(), GitCommit) +}