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.

80 lines
1.6 KiB

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