weed.go 3.2 KB

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