github.com/sagernet/sing-box@v1.9.0-rc.20/cmd/sing-box/cmd_version.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  	"runtime/debug"
     7  
     8  	C "github.com/sagernet/sing-box/constant"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var commandVersion = &cobra.Command{
    14  	Use:   "version",
    15  	Short: "Print current version of sing-box",
    16  	Run:   printVersion,
    17  	Args:  cobra.NoArgs,
    18  }
    19  
    20  var nameOnly bool
    21  
    22  func init() {
    23  	commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only")
    24  	mainCommand.AddCommand(commandVersion)
    25  }
    26  
    27  func printVersion(cmd *cobra.Command, args []string) {
    28  	if nameOnly {
    29  		os.Stdout.WriteString(C.Version + "\n")
    30  		return
    31  	}
    32  	version := "sing-box version " + C.Version + "\n\n"
    33  	version += "Environment: " + runtime.Version() + " " + runtime.GOOS + "/" + runtime.GOARCH + "\n"
    34  
    35  	var tags string
    36  	var revision string
    37  
    38  	debugInfo, loaded := debug.ReadBuildInfo()
    39  	if loaded {
    40  		for _, setting := range debugInfo.Settings {
    41  			switch setting.Key {
    42  			case "-tags":
    43  				tags = setting.Value
    44  			case "vcs.revision":
    45  				revision = setting.Value
    46  			}
    47  		}
    48  	}
    49  
    50  	if tags != "" {
    51  		version += "Tags: " + tags + "\n"
    52  	}
    53  	if revision != "" {
    54  		version += "Revision: " + revision + "\n"
    55  	}
    56  
    57  	if C.CGO_ENABLED {
    58  		version += "CGO: enabled\n"
    59  	} else {
    60  		version += "CGO: disabled\n"
    61  	}
    62  
    63  	os.Stdout.WriteString(version)
    64  }