weed.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. }
  85. exit()
  86. return
  87. }
  88. }
  89. fmt.Fprintf(os.Stderr, "weed: unknown subcommand %q\nRun 'weed help' for usage.\n", args[0])
  90. setExitStatus(2)
  91. exit()
  92. }
  93. var usageTemplate = `
  94. SeaweedFS: store billions of files and serve them fast!
  95. Usage:
  96. weed command [arguments]
  97. The commands are:
  98. {{range .}}{{if .Runnable}}
  99. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  100. Use "weed help [command]" for more information about a command.
  101. `
  102. var helpTemplate = `{{if .Runnable}}Usage: weed {{.UsageLine}}
  103. {{end}}
  104. {{.Long}}
  105. `
  106. // tmpl executes the given template text on data, writing the result to w.
  107. func tmpl(w io.Writer, text string, data interface{}) {
  108. t := template.New("top")
  109. t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
  110. template.Must(t.Parse(text))
  111. if err := t.Execute(w, data); err != nil {
  112. panic(err)
  113. }
  114. }
  115. func capitalize(s string) string {
  116. if s == "" {
  117. return s
  118. }
  119. r, n := utf8.DecodeRuneInString(s)
  120. return string(unicode.ToTitle(r)) + s[n:]
  121. }
  122. func printUsage(w io.Writer) {
  123. tmpl(w, usageTemplate, commands)
  124. }
  125. func usage() {
  126. printUsage(os.Stderr)
  127. fmt.Fprintf(os.Stderr, "For Logging, use \"weed [logging_options] [command]\". The logging options are:\n")
  128. flag.PrintDefaults()
  129. os.Exit(2)
  130. }
  131. // help implements the 'help' command.
  132. func help(args []string) {
  133. if len(args) == 0 {
  134. printUsage(os.Stdout)
  135. // not exit 2: succeeded at 'weed help'.
  136. return
  137. }
  138. if len(args) != 1 {
  139. fmt.Fprintf(os.Stderr, "usage: weed help command\n\nToo many arguments given.\n")
  140. os.Exit(2) // failed at 'weed help'
  141. }
  142. arg := args[0]
  143. for _, cmd := range commands {
  144. if cmd.Name() == arg {
  145. tmpl(os.Stdout, helpTemplate, cmd)
  146. // not exit 2: succeeded at 'weed help cmd'.
  147. return
  148. }
  149. }
  150. fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'weed help'.\n", arg)
  151. os.Exit(2) // failed at 'weed help cmd'
  152. }
  153. var atexitFuncs []func()
  154. func atexit(f func()) {
  155. atexitFuncs = append(atexitFuncs, f)
  156. }
  157. func exit() {
  158. for _, f := range atexitFuncs {
  159. f()
  160. }
  161. os.Exit(exitStatus)
  162. }
  163. func debug(params ...interface{}) {
  164. glog.V(4).Infoln(params...)
  165. }