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.

184 lines
3.4 KiB

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