command.go 986 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package command
  2. //go:generate errorgen
  3. import (
  4. "os"
  5. "github.com/gogo/protobuf/proto"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/infra/conf/serial"
  8. "v2ray.com/core/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. }