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.

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