Browse Source

Refactor: standalone version module

pull/133/head
xjasonlyu 3 years ago
parent
commit
830c231c43
  1. 4
      Makefile
  2. 10
      constant/version.go
  3. 5
      engine/engine.go
  4. 22
      engine/version.go
  5. 2
      stats/server.go
  6. 27
      version/version.go

4
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

10
constant/version.go

@ -1,10 +0,0 @@
package constant
const (
Name = "tun2socks"
)
var (
Version string
GitCommit string
)

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

22
engine/version.go

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

2
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"

27
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)
}
Loading…
Cancel
Save