mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-25 10:01:30 +00:00
Add check/format command
This commit is contained in:
parent
13f41f59d6
commit
718b4afbf3
36
cmd/sing-box/cmd_check.go
Normal file
36
cmd/sing-box/cmd_check.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
"github.com/sagernet/sing-box"
|
||||||
|
"github.com/sagernet/sing-box/option"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var commandCheck = &cobra.Command{
|
||||||
|
Use: "check",
|
||||||
|
Short: "check configuration",
|
||||||
|
Run: checkConfiguration,
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkConfiguration(cmd *cobra.Command, args []string) {
|
||||||
|
configContent, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("read config: ", err)
|
||||||
|
}
|
||||||
|
var options option.Options
|
||||||
|
err = json.Unmarshal(configContent, &options)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("decode config: ", err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
_, err = box.NewService(ctx, options)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("create service: ", err)
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
}
|
61
cmd/sing-box/cmd_format.go
Normal file
61
cmd/sing-box/cmd_format.go
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
|
"github.com/sagernet/sing-box/option"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var commandFormatFlagWrite bool
|
||||||
|
|
||||||
|
var commandFormat = &cobra.Command{
|
||||||
|
Use: "format",
|
||||||
|
Short: "format configuration",
|
||||||
|
Run: formatConfiguration,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
commandFormat.Flags().BoolVarP(&commandFormatFlagWrite, "write", "w", false, "write result to (source) file instead of stdout")
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatConfiguration(cmd *cobra.Command, args []string) {
|
||||||
|
configContent, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("read config: ", err)
|
||||||
|
}
|
||||||
|
var options option.Options
|
||||||
|
err = json.Unmarshal(configContent, &options)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("decode config: ", err)
|
||||||
|
}
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
encoder := json.NewEncoder(buffer)
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
err = encoder.Encode(options)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("encode config: ", err)
|
||||||
|
}
|
||||||
|
if !commandFormatFlagWrite {
|
||||||
|
os.Stdout.Write(buffer.Bytes())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if bytes.Compare(configContent, buffer.Bytes()) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
output, err := os.Create(configPath)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("open output: ", err)
|
||||||
|
}
|
||||||
|
_, err = output.Write(buffer.Bytes())
|
||||||
|
output.Close()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal("write output: ", err)
|
||||||
|
}
|
||||||
|
outputPath, _ := filepath.Abs(configPath)
|
||||||
|
os.Stderr.WriteString(outputPath)
|
||||||
|
}
|
|
@ -35,7 +35,6 @@ func run(cmd *cobra.Command, args []string) {
|
||||||
}
|
}
|
||||||
options.Log.DisableColor = true
|
options.Log.DisableColor = true
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
service, err := box.NewService(ctx, options)
|
service, err := box.NewService(ctx, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -28,6 +28,8 @@ func main() {
|
||||||
command.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
|
command.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
|
||||||
command.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
|
command.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")
|
||||||
command.AddCommand(commandRun)
|
command.AddCommand(commandRun)
|
||||||
|
command.AddCommand(commandCheck)
|
||||||
|
command.AddCommand(commandFormat)
|
||||||
if err := command.Execute(); err != nil {
|
if err := command.Execute(); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue