Files
sing-box-extended-mirror/cmd/sing-box/cmd_version.go
T

65 lines
1.2 KiB
Go
Raw Normal View History

2022-07-04 17:08:28 +08:00
package main
import (
"os"
"runtime"
2022-09-14 12:47:30 +08:00
"runtime/debug"
2022-07-04 17:08:28 +08:00
2022-07-06 15:01:09 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-04 17:08:28 +08:00
"github.com/spf13/cobra"
)
var commandVersion = &cobra.Command{
Use: "version",
Short: "Print current version of sing-box",
Run: printVersion,
2022-08-09 14:49:17 +08:00
Args: cobra.NoArgs,
2022-07-04 17:08:28 +08:00
}
2022-08-12 22:53:46 +08:00
var nameOnly bool
func init() {
commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only")
2022-08-13 18:36:49 +08:00
mainCommand.AddCommand(commandVersion)
2022-08-12 22:53:46 +08:00
}
2022-07-04 17:08:28 +08:00
func printVersion(cmd *cobra.Command, args []string) {
2022-09-14 12:47:30 +08:00
if nameOnly {
os.Stdout.WriteString(C.Version + "\n")
return
2022-08-12 22:53:46 +08:00
}
2022-09-14 12:47:30 +08:00
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
}
2022-08-12 22:53:46 +08:00
}
2022-08-12 15:46:26 +08:00
}
2022-09-14 12:47:30 +08:00
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"
}
2022-08-12 22:53:46 +08:00
os.Stdout.WriteString(version)
2022-07-04 17:08:28 +08:00
}