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.

182 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. "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. cmdFix,
  22. cmdServer,
  23. cmdMaster,
  24. cmdFiler,
  25. cmdUpload,
  26. cmdDownload,
  27. cmdShell,
  28. cmdVersion,
  29. cmdVolume,
  30. cmdExport,
  31. cmdMount,
  32. }
  33. var exitStatus = 0
  34. var exitMu sync.Mutex
  35. func setExitStatus(n int) {
  36. exitMu.Lock()
  37. if exitStatus < n {
  38. exitStatus = n
  39. }
  40. exitMu.Unlock()
  41. }
  42. func main() {
  43. glog.MaxSize = 1024 * 1024 * 32
  44. rand.Seed(time.Now().UnixNano())
  45. flag.Usage = usage
  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. Weed File System : 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. }