command.go 1.4 KB

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