command.go 1.5 KB

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