weed.go 3.5 KB

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