command.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 xray command
  15. // like xray run or xray 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 the first word (the "executable name") and the first flag or argument in the line are taken to be the command name.
  22. //
  23. // UsageLine supports go template syntax. It's recommended to use "{{.Exec}}" instead of hardcoding name
  24. UsageLine string
  25. // Short is the short description shown in the 'go help' output.
  26. //
  27. // Note: Short does not support go template syntax.
  28. Short string
  29. // Long is the long message shown in the 'go help <this-command>' output.
  30. //
  31. // Long supports go template syntax. It's recommended to use "{{.Exec}}", "{{.LongName}}" instead of hardcoding strings
  32. Long string
  33. // Flag is a set of flags specific to this command.
  34. Flag flag.FlagSet
  35. // CustomFlags indicates that the command will do its own
  36. // flag parsing.
  37. CustomFlags bool
  38. // Commands lists the available commands and help topics.
  39. // The order here is the order in which they are printed by 'go help'.
  40. // Note that subcommands are in general best avoided.
  41. Commands []*Command
  42. }
  43. // LongName returns the command's long name: all the words in the usage line between first word (e.g. "xray") and a flag or argument,
  44. func (c *Command) LongName() string {
  45. name := c.UsageLine
  46. if i := strings.Index(name, " ["); i >= 0 {
  47. name = strings.TrimSpace(name[:i])
  48. }
  49. if i := strings.Index(name, " "); i >= 0 {
  50. name = name[i+1:]
  51. } else {
  52. name = ""
  53. }
  54. return strings.TrimSpace(name)
  55. }
  56. // Name returns the command's short name: the last word in the usage line before a flag or argument.
  57. func (c *Command) Name() string {
  58. name := c.LongName()
  59. if i := strings.LastIndex(name, " "); i >= 0 {
  60. name = name[i+1:]
  61. }
  62. return strings.TrimSpace(name)
  63. }
  64. // Usage prints usage of the Command
  65. func (c *Command) Usage() {
  66. buildCommandText(c)
  67. fmt.Fprintf(os.Stderr, "usage: %s\n", c.UsageLine)
  68. fmt.Fprintf(os.Stderr, "Run 'xray help %s' for details.\n", c.LongName())
  69. SetExitStatus(2)
  70. Exit()
  71. }
  72. // Runnable reports whether the command can be run; otherwise
  73. // it is a documentation pseudo-command such as importpath.
  74. func (c *Command) Runnable() bool {
  75. return c.Run != nil
  76. }
  77. // Exit exits with code set with SetExitStatus()
  78. func Exit() {
  79. os.Exit(exitStatus)
  80. }
  81. // Fatalf logs error and exit with code 1
  82. func Fatalf(format string, args ...interface{}) {
  83. Errorf(format, args...)
  84. Exit()
  85. }
  86. // Errorf logs error and set exit status to 1, but not exit
  87. func Errorf(format string, args ...interface{}) {
  88. fmt.Fprintf(os.Stderr, format, args...)
  89. fmt.Fprintln(os.Stderr)
  90. SetExitStatus(1)
  91. }
  92. // ExitIfErrors exits if current status is not zero
  93. func ExitIfErrors() {
  94. if exitStatus != 0 {
  95. Exit()
  96. }
  97. }
  98. var (
  99. exitStatus = 0
  100. exitMu sync.Mutex
  101. )
  102. // SetExitStatus set exit status code
  103. func SetExitStatus(n int) {
  104. exitMu.Lock()
  105. if exitStatus < n {
  106. exitStatus = n
  107. }
  108. exitMu.Unlock()
  109. }
  110. // GetExitStatus get exit status code
  111. func GetExitStatus() int {
  112. return exitStatus
  113. }