Add version command

This commit is contained in:
世界 2022-07-04 17:08:28 +08:00
parent 718b4afbf3
commit 792dc83778
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
5 changed files with 42 additions and 16 deletions

View File

@ -13,7 +13,7 @@ import (
var commandCheck = &cobra.Command{
Use: "check",
Short: "check configuration",
Short: "Check configuration",
Run: checkConfiguration,
}

View File

@ -15,7 +15,7 @@ var commandFormatFlagWrite bool
var commandFormat = &cobra.Command{
Use: "format",
Short: "format configuration",
Short: "Format configuration",
Run: formatConfiguration,
}
@ -41,7 +41,7 @@ func formatConfiguration(cmd *cobra.Command, args []string) {
logrus.Fatal("encode config: ", err)
}
if !commandFormatFlagWrite {
os.Stdout.Write(buffer.Bytes())
os.Stdout.WriteString(buffer.String() + "\n")
return
}
if bytes.Compare(configContent, buffer.Bytes()) == 0 {
@ -57,5 +57,5 @@ func formatConfiguration(cmd *cobra.Command, args []string) {
logrus.Fatal("write output: ", err)
}
outputPath, _ := filepath.Abs(configPath)
os.Stderr.WriteString(outputPath)
os.Stderr.WriteString(outputPath + "\n")
}

View File

@ -15,7 +15,7 @@ import (
var commandRun = &cobra.Command{
Use: "run",
Short: "run service",
Short: "Run service",
Run: run,
}

View File

@ -0,0 +1,20 @@
package main
import (
"os"
"runtime"
C "github.com/sagernet/sing-box/constant"
F "github.com/sagernet/sing/common/format"
"github.com/spf13/cobra"
)
var commandVersion = &cobra.Command{
Use: "version",
Short: "Print current version of sing-box",
Run: printVersion,
}
func printVersion(cmd *cobra.Command, args []string) {
os.Stderr.WriteString(F.ToString("sing-box version ", C.Version, " (", runtime.Version(), " ", runtime.GOOS, "/", runtime.GOARCH, ")\n"))
}

View File

@ -19,18 +19,24 @@ var (
disableColor bool
)
var mainCommand = &cobra.Command{
Use: "sing-box",
PersistentPreRun: preRun,
}
func init() {
mainCommand.PersistentFlags().StringVarP(&configPath, "config", "c", "config.json", "set configuration file path")
mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
mainCommand.AddCommand(commandRun)
mainCommand.AddCommand(commandCheck)
mainCommand.AddCommand(commandFormat)
mainCommand.AddCommand(commandVersion)
}
func main() {
command := &cobra.Command{
Use: "sing-box",
PersistentPreRun: preRun,
}
command.PersistentFlags().StringVarP(&configPath, "config", "c", "config.json", "set configuration file path")
command.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
command.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
command.AddCommand(commandRun)
command.AddCommand(commandCheck)
command.AddCommand(commandFormat)
if err := command.Execute(); err != nil {
if err := mainCommand.Execute(); err != nil {
logrus.Fatal(err)
}
}