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{ var commandCheck = &cobra.Command{
Use: "check", Use: "check",
Short: "check configuration", Short: "Check configuration",
Run: checkConfiguration, Run: checkConfiguration,
} }

View file

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

View file

@ -15,7 +15,7 @@ import (
var commandRun = &cobra.Command{ var commandRun = &cobra.Command{
Use: "run", Use: "run",
Short: "run service", Short: "Run service",
Run: run, 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 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() { func main() {
command := &cobra.Command{ if err := mainCommand.Execute(); err != nil {
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 {
logrus.Fatal(err) logrus.Fatal(err)
} }
} }