github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/command/command.go (about)

     1  package command
     2  
     3  //go:generate go run v2ray.com/core/common/errors/errorgen
     4  
     5  import (
     6  	"os"
     7  
     8  	"github.com/golang/protobuf/proto"
     9  	"v2ray.com/core/common"
    10  	"v2ray.com/core/infra/conf/serial"
    11  	"v2ray.com/core/infra/control"
    12  )
    13  
    14  type ConfigCommand struct{}
    15  
    16  func (c *ConfigCommand) Name() string {
    17  	return "config"
    18  }
    19  
    20  func (c *ConfigCommand) Description() control.Description {
    21  	return control.Description{
    22  		Short: "Convert config among different formats.",
    23  		Usage: []string{
    24  			"v2ctl config",
    25  		},
    26  	}
    27  }
    28  
    29  func (c *ConfigCommand) Execute(args []string) error {
    30  	pbConfig, err := serial.LoadJSONConfig(os.Stdin)
    31  	if err != nil {
    32  		return newError("failed to parse json config").Base(err)
    33  	}
    34  
    35  	bytesConfig, err := proto.Marshal(pbConfig)
    36  	if err != nil {
    37  		return newError("failed to marshal proto config").Base(err)
    38  	}
    39  
    40  	if _, err := os.Stdout.Write(bytesConfig); err != nil {
    41  		return newError("failed to write proto config").Base(err)
    42  	}
    43  	return nil
    44  }
    45  
    46  func init() {
    47  	common.Must(control.RegisterCommand(&ConfigCommand{}))
    48  }