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) +}