github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/main/commands/all/engineering/convertpb.go (about)

     1  package engineering
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"google.golang.org/protobuf/proto"
    10  
    11  	core "github.com/v2fly/v2ray-core/v5"
    12  	"github.com/v2fly/v2ray-core/v5/common/cmdarg"
    13  	"github.com/v2fly/v2ray-core/v5/main/commands/base"
    14  )
    15  
    16  var (
    17  	configFiles          cmdarg.Arg
    18  	configDirs           cmdarg.Arg
    19  	configFormat         *string
    20  	configDirRecursively *bool
    21  )
    22  
    23  func setConfigFlags(cmd *base.Command) {
    24  	configFormat = cmd.Flag.String("format", core.FormatAuto, "")
    25  	configDirRecursively = cmd.Flag.Bool("r", false, "")
    26  
    27  	cmd.Flag.Var(&configFiles, "config", "")
    28  	cmd.Flag.Var(&configFiles, "c", "")
    29  	cmd.Flag.Var(&configDirs, "confdir", "")
    30  	cmd.Flag.Var(&configDirs, "d", "")
    31  }
    32  
    33  var cmdConvertPb = &base.Command{
    34  	UsageLine:   "{{.Exec}} engineering convertpb [-c config.json] [-d dir]",
    35  	CustomFlags: true,
    36  	Run: func(cmd *base.Command, args []string) {
    37  		setConfigFlags(cmd)
    38  		cmd.Flag.Parse(args)
    39  		config, err := core.LoadConfig(*configFormat, configFiles)
    40  		if err != nil {
    41  			if len(configFiles) == 0 {
    42  				base.Fatalf("%s", newError("failed to load config").Base(err))
    43  				return
    44  			}
    45  			base.Fatalf("%s", newError(fmt.Sprintf("failed to load config: %s", configFiles)).Base(err))
    46  			return
    47  		}
    48  		bytew, err := proto.Marshal(config)
    49  		if err != nil {
    50  			base.Fatalf("%s", newError("failed to marshal config").Base(err))
    51  			return
    52  		}
    53  		io.Copy(os.Stdout, bytes.NewReader(bytew))
    54  	},
    55  }