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.

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