command.go 1014 B

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