command.go 1.1 KB

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