Add check/format command

This commit is contained in:
世界
2022-07-04 16:59:27 +08:00
parent 13f41f59d6
commit 718b4afbf3
4 changed files with 99 additions and 1 deletions

36
cmd/sing-box/cmd_check.go Normal file
View 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()
}