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.

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