command.go 1.5 KB

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