command.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package base defines shared basic pieces of the commands,
  5. // in particular logging and the Command structure.
  6. package base
  7. import (
  8. "flag"
  9. "fmt"
  10. "os"
  11. "strings"
  12. "sync"
  13. )
  14. // A Command is an implementation of a v2ray command
  15. // like v2ray run or v2ray version.
  16. type Command struct {
  17. // Run runs the command.
  18. // The args are the arguments after the command name.
  19. Run func(cmd *Command, args []string)
  20. // UsageLine is the one-line usage message.
  21. // The words between "go" and the first flag or argument in the line are taken to be the command name.
  22. UsageLine string
  23. // Short is the short description shown in the 'go help' output.
  24. Short string
  25. // Long is the long message shown in the 'go help <this-command>' output.
  26. Long string
  27. // Flag is a set of flags specific to this command.
  28. Flag flag.FlagSet
  29. // CustomFlags indicates that the command will do its own
  30. // flag parsing.
  31. CustomFlags bool
  32. // Commands lists the available commands and help topics.
  33. // The order here is the order in which they are printed by 'go help'.
  34. // Note that subcommands are in general best avoided.
  35. Commands []*Command
  36. }
  37. // LongName returns the command's long name: all the words in the usage line between "go" and a flag or argument,
  38. func (c *Command) LongName() string {
  39. name := c.UsageLine
  40. if i := strings.Index(name, " ["); i >= 0 {
  41. name = name[:i]
  42. }
  43. if name == CommandEnv.Exec {
  44. return ""
  45. }
  46. return strings.TrimPrefix(name, CommandEnv.Exec+" ")
  47. }
  48. // Name returns the command's short name: the last word in the usage line before a flag or argument.
  49. func (c *Command) Name() string {
  50. name := c.LongName()
  51. if i := strings.LastIndex(name, " "); i >= 0 {
  52. name = name[i+1:]
  53. }
  54. return name
  55. }
  56. // Usage prints usage of the Command
  57. func (c *Command) Usage() {
  58. fmt.Fprintf(os.Stderr, "usage: %s\n", c.UsageLine)
  59. fmt.Fprintf(os.Stderr, "Run '%s help %s' for details.\n", CommandEnv.Exec, c.LongName())
  60. SetExitStatus(2)
  61. Exit()
  62. }
  63. // Runnable reports whether the command can be run; otherwise
  64. // it is a documentation pseudo-command such as importpath.
  65. func (c *Command) Runnable() bool {
  66. return c.Run != nil
  67. }
  68. // Exit exits with code set with SetExitStatus()
  69. func Exit() {
  70. os.Exit(exitStatus)
  71. }
  72. // Fatalf logs error and exit with code 1
  73. func Fatalf(format string, args ...interface{}) {
  74. Errorf(format, args...)
  75. Exit()
  76. }
  77. // Errorf logs error and set exit status to 1, but not exit
  78. func Errorf(format string, args ...interface{}) {
  79. fmt.Fprintf(os.Stderr, format, args...)
  80. fmt.Fprintln(os.Stderr)
  81. SetExitStatus(1)
  82. }
  83. // ExitIfErrors exits if current status is not zero
  84. func ExitIfErrors() {
  85. if exitStatus != 0 {
  86. Exit()
  87. }
  88. }
  89. var exitStatus = 0
  90. var exitMu sync.Mutex
  91. // SetExitStatus set exit status code
  92. func SetExitStatus(n int) {
  93. exitMu.Lock()
  94. if exitStatus < n {
  95. exitStatus = n
  96. }
  97. exitMu.Unlock()
  98. }
  99. // GetExitStatus get exit status code
  100. func GetExitStatus() int {
  101. return exitStatus
  102. }