You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
3.7 KiB

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