command.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package command
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. var Commands = []*Command{
  9. cmdBenchmark,
  10. cmdBackup,
  11. cmdCompact,
  12. cmdCopy,
  13. cmdFix,
  14. cmdFilerReplicate,
  15. cmdServer,
  16. cmdMaster,
  17. cmdFiler,
  18. cmdS3,
  19. cmdUpload,
  20. cmdDownload,
  21. cmdMsgBroker,
  22. cmdScaffold,
  23. cmdShell,
  24. cmdVersion,
  25. cmdVolume,
  26. cmdExport,
  27. cmdMount,
  28. cmdWebDav,
  29. }
  30. type Command struct {
  31. // Run runs the command.
  32. // The args are the arguments after the command name.
  33. Run func(cmd *Command, args []string) bool
  34. // UsageLine is the one-line usage message.
  35. // The first word in the line is taken to be the command name.
  36. UsageLine string
  37. // Short is the short description shown in the 'go help' output.
  38. Short string
  39. // Long is the long message shown in the 'go help <this-command>' output.
  40. Long string
  41. // Flag is a set of flags specific to this command.
  42. Flag flag.FlagSet
  43. IsDebug *bool
  44. }
  45. // Name returns the command's name: the first word in the usage line.
  46. func (c *Command) Name() string {
  47. name := c.UsageLine
  48. i := strings.Index(name, " ")
  49. if i >= 0 {
  50. name = name[:i]
  51. }
  52. return name
  53. }
  54. func (c *Command) Usage() {
  55. fmt.Fprintf(os.Stderr, "Example: weed %s\n", c.UsageLine)
  56. fmt.Fprintf(os.Stderr, "Default Usage:\n")
  57. c.Flag.PrintDefaults()
  58. fmt.Fprintf(os.Stderr, "Description:\n")
  59. fmt.Fprintf(os.Stderr, " %s\n", strings.TrimSpace(c.Long))
  60. os.Exit(2)
  61. }
  62. // Runnable reports whether the command can be run; otherwise
  63. // it is a documentation pseudo-command such as importpath.
  64. func (c *Command) Runnable() bool {
  65. return c.Run != nil
  66. }