weed.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. ProfilesSampleRate: 0.1,
  48. })
  49. if err != nil {
  50. fmt.Fprintf(os.Stderr, "sentry.Init: %v", err)
  51. }
  52. // Flush buffered events before the program terminates.
  53. // Set the timeout to the maximum duration the program can afford to wait.
  54. defer sentry.Flush(2 * time.Second)
  55. if command.AutocompleteMain(commands) {
  56. return
  57. }
  58. flag.Parse()
  59. args := flag.Args()
  60. if len(args) < 1 {
  61. usage()
  62. }
  63. if args[0] == "help" {
  64. help(args[1:])
  65. for _, cmd := range commands {
  66. if len(args) >= 2 && cmd.Name() == args[1] && cmd.Run != nil {
  67. fmt.Fprintf(os.Stderr, "Default Parameters:\n")
  68. cmd.Flag.PrintDefaults()
  69. }
  70. }
  71. return
  72. }
  73. util_http.InitGlobalHttpClient()
  74. for _, cmd := range commands {
  75. if cmd.Name() == args[0] && cmd.Run != nil {
  76. cmd.Flag.Usage = func() { cmd.Usage() }
  77. cmd.Flag.Parse(args[1:])
  78. args = cmd.Flag.Args()
  79. IsDebug = cmd.IsDebug
  80. if !cmd.Run(cmd, args) {
  81. fmt.Fprintf(os.Stderr, "\n")
  82. cmd.Flag.Usage()
  83. fmt.Fprintf(os.Stderr, "Default Parameters:\n")
  84. cmd.Flag.PrintDefaults()
  85. }
  86. exit()
  87. return
  88. }
  89. }
  90. fmt.Fprintf(os.Stderr, "weed: unknown subcommand %q\nRun 'weed help' for usage.\n", args[0])
  91. setExitStatus(2)
  92. exit()
  93. }
  94. var usageTemplate = `
  95. SeaweedFS: store billions of files and serve them fast!
  96. Usage:
  97. weed command [arguments]
  98. The commands are:
  99. {{range .}}{{if .Runnable}}
  100. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  101. Use "weed help [command]" for more information about a command.
  102. `
  103. var helpTemplate = `{{if .Runnable}}Usage: weed {{.UsageLine}}
  104. {{end}}
  105. {{.Long}}
  106. `
  107. // tmpl executes the given template text on data, writing the result to w.
  108. func tmpl(w io.Writer, text string, data interface{}) {
  109. t := template.New("top")
  110. t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
  111. template.Must(t.Parse(text))
  112. if err := t.Execute(w, data); err != nil {
  113. panic(err)
  114. }
  115. }
  116. func capitalize(s string) string {
  117. if s == "" {
  118. return s
  119. }
  120. r, n := utf8.DecodeRuneInString(s)
  121. return string(unicode.ToTitle(r)) + s[n:]
  122. }
  123. func printUsage(w io.Writer) {
  124. tmpl(w, usageTemplate, commands)
  125. }
  126. func usage() {
  127. printUsage(os.Stderr)
  128. fmt.Fprintf(os.Stderr, "For Logging, use \"weed [logging_options] [command]\". The logging options are:\n")
  129. flag.PrintDefaults()
  130. os.Exit(2)
  131. }
  132. // help implements the 'help' command.
  133. func help(args []string) {
  134. if len(args) == 0 {
  135. printUsage(os.Stdout)
  136. // not exit 2: succeeded at 'weed help'.
  137. return
  138. }
  139. if len(args) != 1 {
  140. fmt.Fprintf(os.Stderr, "usage: weed help command\n\nToo many arguments given.\n")
  141. os.Exit(2) // failed at 'weed help'
  142. }
  143. arg := args[0]
  144. for _, cmd := range commands {
  145. if cmd.Name() == arg {
  146. tmpl(os.Stdout, helpTemplate, cmd)
  147. // not exit 2: succeeded at 'weed help cmd'.
  148. return
  149. }
  150. }
  151. fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'weed help'.\n", arg)
  152. os.Exit(2) // failed at 'weed help cmd'
  153. }
  154. var atexitFuncs []func()
  155. func atexit(f func()) {
  156. atexitFuncs = append(atexitFuncs, f)
  157. }
  158. func exit() {
  159. for _, f := range atexitFuncs {
  160. f()
  161. }
  162. os.Exit(exitStatus)
  163. }
  164. func debug(params ...interface{}) {
  165. glog.V(4).Infoln(params...)
  166. }