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.

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