weed.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package main
  2. import (
  3. "embed"
  4. "fmt"
  5. "io"
  6. "io/fs"
  7. "os"
  8. "strings"
  9. "sync"
  10. "text/template"
  11. "time"
  12. "unicode"
  13. "unicode/utf8"
  14. weed_server "github.com/seaweedfs/seaweedfs/weed/server"
  15. "github.com/seaweedfs/seaweedfs/weed/util"
  16. flag "github.com/seaweedfs/seaweedfs/weed/util/fla9"
  17. "github.com/getsentry/sentry-go"
  18. "github.com/seaweedfs/seaweedfs/weed/command"
  19. "github.com/seaweedfs/seaweedfs/weed/glog"
  20. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  21. )
  22. var IsDebug *bool
  23. var commands = command.Commands
  24. var exitStatus = 0
  25. var exitMu sync.Mutex
  26. func setExitStatus(n int) {
  27. exitMu.Lock()
  28. if exitStatus < n {
  29. exitStatus = n
  30. }
  31. exitMu.Unlock()
  32. }
  33. //go:embed static
  34. var static embed.FS
  35. func init() {
  36. weed_server.StaticFS, _ = fs.Sub(static, "static")
  37. flag.Var(&util.ConfigurationFileDirectory, "config_dir", "directory with toml configuration files")
  38. }
  39. func main() {
  40. glog.MaxSize = 1024 * 1024 * 10
  41. glog.MaxFileCount = 5
  42. flag.Usage = usage
  43. err := sentry.Init(sentry.ClientOptions{
  44. SampleRate: 0.1,
  45. EnableTracing: true,
  46. TracesSampleRate: 0.1,
  47. })
  48. if err != nil {
  49. fmt.Fprintf(os.Stderr, "sentry.Init: %v", err)
  50. }
  51. // Flush buffered events before the program terminates.
  52. // Set the timeout to the maximum duration the program can afford to wait.
  53. defer sentry.Flush(2 * time.Second)
  54. if command.AutocompleteMain(commands) {
  55. return
  56. }
  57. flag.Parse()
  58. args := flag.Args()
  59. if len(args) < 1 {
  60. usage()
  61. }
  62. if args[0] == "help" {
  63. help(args[1:])
  64. for _, cmd := range commands {
  65. if len(args) >= 2 && cmd.Name() == args[1] && cmd.Run != nil {
  66. fmt.Fprintf(os.Stderr, "Default Parameters:\n")
  67. cmd.Flag.PrintDefaults()
  68. }
  69. }
  70. return
  71. }
  72. util_http.InitGlobalHttpClient()
  73. for _, cmd := range commands {
  74. if cmd.Name() == args[0] && cmd.Run != nil {
  75. cmd.Flag.Usage = func() { cmd.Usage() }
  76. cmd.Flag.Parse(args[1:])
  77. args = cmd.Flag.Args()
  78. IsDebug = cmd.IsDebug
  79. if !cmd.Run(cmd, args) {
  80. fmt.Fprintf(os.Stderr, "\n")
  81. cmd.Flag.Usage()
  82. fmt.Fprintf(os.Stderr, "Default Parameters:\n")
  83. cmd.Flag.PrintDefaults()
  84. // Command execution failed - general error
  85. setExitStatus(1)
  86. }
  87. exit()
  88. return
  89. }
  90. }
  91. // Unknown command - syntax error
  92. fmt.Fprintf(os.Stderr, "weed: unknown subcommand %q\nRun 'weed help' for usage.\n", args[0])
  93. setExitStatus(2)
  94. exit()
  95. }
  96. var usageTemplate = `
  97. SeaweedFS: store billions of files and serve them fast!
  98. Usage:
  99. weed command [arguments]
  100. The commands are:
  101. {{range .}}{{if .Runnable}}
  102. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  103. Use "weed help [command]" for more information about a command.
  104. `
  105. var helpTemplate = `{{if .Runnable}}Usage: weed {{.UsageLine}}
  106. {{end}}
  107. {{.Long}}
  108. `
  109. // tmpl executes the given template text on data, writing the result to w.
  110. func tmpl(w io.Writer, text string, data interface{}) {
  111. t := template.New("top")
  112. t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
  113. template.Must(t.Parse(text))
  114. if err := t.Execute(w, data); err != nil {
  115. panic(err)
  116. }
  117. }
  118. func capitalize(s string) string {
  119. if s == "" {
  120. return s
  121. }
  122. r, n := utf8.DecodeRuneInString(s)
  123. return string(unicode.ToTitle(r)) + s[n:]
  124. }
  125. func printUsage(w io.Writer) {
  126. tmpl(w, usageTemplate, commands)
  127. }
  128. func usage() {
  129. printUsage(os.Stderr)
  130. fmt.Fprintf(os.Stderr, "For Logging, use \"weed [logging_options] [command]\". The logging options are:\n")
  131. flag.PrintDefaults()
  132. // Invalid command line usage - syntax error
  133. setExitStatus(2)
  134. exit()
  135. }
  136. // help implements the 'help' command.
  137. func help(args []string) {
  138. if len(args) == 0 {
  139. printUsage(os.Stdout)
  140. // Success - help displayed correctly
  141. return
  142. }
  143. if len(args) != 1 {
  144. fmt.Fprintf(os.Stderr, "usage: weed help command\n\nToo many arguments given.\n")
  145. // Invalid help usage - syntax error
  146. setExitStatus(2)
  147. exit()
  148. }
  149. arg := args[0]
  150. for _, cmd := range commands {
  151. if cmd.Name() == arg {
  152. tmpl(os.Stdout, helpTemplate, cmd)
  153. // Success - help for specific command displayed correctly
  154. return
  155. }
  156. }
  157. fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'weed help'.\n", arg)
  158. // Unknown help topic - syntax error
  159. setExitStatus(2)
  160. exit()
  161. }
  162. var atexitFuncs []func()
  163. func atexit(f func()) {
  164. atexitFuncs = append(atexitFuncs, f)
  165. }
  166. func exit() {
  167. for _, f := range atexitFuncs {
  168. f()
  169. }
  170. os.Exit(exitStatus)
  171. }
  172. func debug(params ...interface{}) {
  173. glog.V(4).Infoln(params...)
  174. }