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.

75 lines
1.5 KiB

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