github.com/sagernet/sing-box@v1.9.0-rc.20/cmd/sing-box/cmd_check.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/sagernet/sing-box"
     7  	"github.com/sagernet/sing-box/log"
     8  
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var commandCheck = &cobra.Command{
    13  	Use:   "check",
    14  	Short: "Check configuration",
    15  	Run: func(cmd *cobra.Command, args []string) {
    16  		err := check()
    17  		if err != nil {
    18  			log.Fatal(err)
    19  		}
    20  	},
    21  	Args: cobra.NoArgs,
    22  }
    23  
    24  func init() {
    25  	mainCommand.AddCommand(commandCheck)
    26  }
    27  
    28  func check() error {
    29  	options, err := readConfigAndMerge()
    30  	if err != nil {
    31  		return err
    32  	}
    33  	ctx, cancel := context.WithCancel(context.Background())
    34  	instance, err := box.New(box.Options{
    35  		Context: ctx,
    36  		Options: options,
    37  	})
    38  	if err == nil {
    39  		instance.Close()
    40  	}
    41  	cancel()
    42  	return err
    43  }