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.

181 lines
3.3 KiB

11 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "os"
  9. "strings"
  10. "sync"
  11. "text/template"
  12. "time"
  13. "unicode"
  14. "unicode/utf8"
  15. )
  16. var IsDebug *bool
  17. var server *string
  18. var commands = []*Command{
  19. cmdBenchmark,
  20. cmdCompact,
  21. cmdFiler,
  22. cmdFix,
  23. cmdServer,
  24. cmdMaster,
  25. cmdUpload,
  26. cmdDownload,
  27. cmdShell,
  28. cmdVersion,
  29. cmdVolume,
  30. cmdExport,
  31. }
  32. var exitStatus = 0
  33. var exitMu sync.Mutex
  34. func setExitStatus(n int) {
  35. exitMu.Lock()
  36. if exitStatus < n {
  37. exitStatus = n
  38. }
  39. exitMu.Unlock()
  40. }
  41. func main() {
  42. glog.MaxSize = 1024 * 1024 * 32
  43. rand.Seed(time.Now().UnixNano())
  44. flag.Usage = usage
  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. Weed File System : 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. }