cmd_check.go 774 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "github.com/sagernet/sing-box"
  6. "github.com/sagernet/sing-box/option"
  7. "github.com/goccy/go-json"
  8. "github.com/sirupsen/logrus"
  9. "github.com/spf13/cobra"
  10. )
  11. var commandCheck = &cobra.Command{
  12. Use: "check",
  13. Short: "Check configuration",
  14. Run: checkConfiguration,
  15. }
  16. func checkConfiguration(cmd *cobra.Command, args []string) {
  17. configContent, err := os.ReadFile(configPath)
  18. if err != nil {
  19. logrus.Fatal("read config: ", err)
  20. }
  21. var options option.Options
  22. err = json.Unmarshal(configContent, &options)
  23. if err != nil {
  24. logrus.Fatal("decode config: ", err)
  25. }
  26. ctx, cancel := context.WithCancel(context.Background())
  27. _, err = box.New(ctx, options)
  28. if err != nil {
  29. logrus.Fatal("create service: ", err)
  30. }
  31. cancel()
  32. }