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.

186 lines
3.6 KiB

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