github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/command/command.go (about)

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