فهرست منبع

Print tags in version command

世界 3 سال پیش
والد
کامیت
9aa7a20d96
5فایلهای تغییر یافته به همراه37 افزوده شده و 32 حذف شده
  1. 1 1
      .goreleaser.yaml
  2. 1 1
      Dockerfile
  3. 1 3
      Makefile
  4. 33 23
      cmd/sing-box/cmd_version.go
  5. 1 4
      constant/version.go

+ 1 - 1
.goreleaser.yaml

@@ -9,7 +9,7 @@ builds:
     gcflags:
       - all=-trimpath={{.Env.GOPATH}}
     ldflags:
-      - -X github.com/sagernet/sing-box/constant.Commit={{ .ShortCommit }} -s -w -buildid=
+      - -s -w -buildid=
     tags:
       - with_quic
       - with_wireguard

+ 1 - 1
Dockerfile

@@ -10,7 +10,7 @@ RUN set -ex \
     && export COMMIT=$(git rev-parse --short HEAD) \
     && go build -v -trimpath -tags 'no_gvisor,with_quic,with_wireguard,with_acme' \
         -o /go/bin/sing-box \
-        -ldflags "-X github.com/sagernet/sing-box/constant.Commit=${COMMIT} -w -s -buildid=" \
+        -ldflags "-s -w -buildid=" \
         ./cmd/sing-box
 FROM alpine AS dist
 LABEL maintainer="nekohasekai <[email protected]>"

+ 1 - 3
Makefile

@@ -1,9 +1,7 @@
 NAME = sing-box
 COMMIT = $(shell git rev-parse --short HEAD)
 TAGS ?= with_quic,with_wireguard,with_clash_api
-PARAMS = -v -trimpath -tags '$(TAGS)' -ldflags \
-		'-X "github.com/sagernet/sing-box/constant.Commit=$(COMMIT)" \
-		-w -s -buildid='
+PARAMS = -v -trimpath -tags '$(TAGS)' -ldflags '-s -w -buildid='
 MAIN = ./cmd/sing-box
 
 .PHONY: test release

+ 33 - 23
cmd/sing-box/cmd_version.go

@@ -3,9 +3,9 @@ package main
 import (
 	"os"
 	"runtime"
+	"runtime/debug"
 
 	C "github.com/sagernet/sing-box/constant"
-	F "github.com/sagernet/sing/common/format"
 
 	"github.com/spf13/cobra"
 )
@@ -25,30 +25,40 @@ func init() {
 }
 
 func printVersion(cmd *cobra.Command, args []string) {
-	var version string
-	if !nameOnly {
-		version = "sing-box "
+	if nameOnly {
+		os.Stdout.WriteString(C.Version + "\n")
+		return
 	}
-	version += F.ToString(C.Version)
-	if C.Commit != "" {
-		version += "." + C.Commit
-	}
-	if !nameOnly {
-		version += " ("
-		version += runtime.Version()
-		version += ", "
-		version += runtime.GOOS
-		version += "/"
-		version += runtime.GOARCH
-		version += ", "
-		version += "CGO "
-		if C.CGO_ENABLED {
-			version += "enabled"
-		} else {
-			version += "disabled"
+	version := "sing-box version " + C.Version + "\n\n"
+	version += "Environment: " + runtime.Version() + " " + runtime.GOOS + "/" + runtime.GOARCH + "\n"
+
+	var tags string
+	var revision string
+
+	debugInfo, loaded := debug.ReadBuildInfo()
+	if loaded {
+		for _, setting := range debugInfo.Settings {
+			switch setting.Key {
+			case "-tags":
+				tags = setting.Value
+			case "vcs.revision":
+				revision = setting.Value
+			}
 		}
-		version += ")"
 	}
-	version += "\n"
+
+	if tags != "" {
+		version += "Tags: " + tags + "\n"
+	}
+	if revision != "" {
+		version += "Revision: " + revision + "\n"
+	}
+
+	if C.CGO_ENABLED {
+		version += "CGO: enabled\n"
+	} else {
+		version += "CGO: disabled\n"
+	}
+
 	os.Stdout.WriteString(version)
 }

+ 1 - 4
constant/version.go

@@ -1,6 +1,3 @@
 package constant
 
-var (
-	Version = "1.1-beta4"
-	Commit  = ""
-)
+var Version = "1.1-beta4"