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.

173 lines
3.3 KiB

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