diff --git a/cmd/sing-box/cmd_check.go b/cmd/sing-box/cmd_check.go index 55a2911a..559d413c 100644 --- a/cmd/sing-box/cmd_check.go +++ b/cmd/sing-box/cmd_check.go @@ -13,7 +13,7 @@ import ( var commandCheck = &cobra.Command{ Use: "check", - Short: "check configuration", + Short: "Check configuration", Run: checkConfiguration, } diff --git a/cmd/sing-box/cmd_format.go b/cmd/sing-box/cmd_format.go index 5dc78806..fd2c6e63 100644 --- a/cmd/sing-box/cmd_format.go +++ b/cmd/sing-box/cmd_format.go @@ -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") } diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index 63ab9689..d7df3762 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -15,7 +15,7 @@ import ( var commandRun = &cobra.Command{ Use: "run", - Short: "run service", + Short: "Run service", Run: run, } diff --git a/cmd/sing-box/cmd_version.go b/cmd/sing-box/cmd_version.go new file mode 100644 index 00000000..ee399d33 --- /dev/null +++ b/cmd/sing-box/cmd_version.go @@ -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")) +} diff --git a/cmd/sing-box/main.go b/cmd/sing-box/main.go index 55770199..bda56fd2 100644 --- a/cmd/sing-box/main.go +++ b/cmd/sing-box/main.go @@ -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) } }