main.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "github.com/v2fly/v2ray-core/v4/common/log"
  7. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/memconservative"
  8. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
  9. "github.com/v2fly/v2ray-core/v4/infra/control"
  10. )
  11. func getCommandName() string {
  12. if len(os.Args) > 1 {
  13. return os.Args[1]
  14. }
  15. return ""
  16. }
  17. func main() {
  18. // let the v2ctl prints log at stderr
  19. log.RegisterHandler(log.NewLogger(log.CreateStderrLogWriter()))
  20. name := getCommandName()
  21. cmd := control.GetCommand(name)
  22. if cmd == nil {
  23. fmt.Fprintln(os.Stderr, "Unknown command:", name)
  24. fmt.Fprintln(os.Stderr)
  25. fmt.Println("v2ctl <command>")
  26. fmt.Println("Available commands:")
  27. control.PrintUsage()
  28. return
  29. }
  30. if err := cmd.Execute(os.Args[2:]); err != nil {
  31. hasError := false
  32. if err != flag.ErrHelp {
  33. fmt.Fprintln(os.Stderr, err.Error())
  34. fmt.Fprintln(os.Stderr)
  35. hasError = true
  36. }
  37. for _, line := range cmd.Description().Usage {
  38. fmt.Println(line)
  39. }
  40. if hasError {
  41. os.Exit(-1)
  42. }
  43. }
  44. }