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.

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