weed.go 3.5 KB

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