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.

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