mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Add version command
This commit is contained in:
parent
718b4afbf3
commit
792dc83778
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
cmd/sing-box/cmd_version.go
Normal file
20
cmd/sing-box/cmd_version.go
Normal 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"))
|
||||||
|
}
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue