command.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. cmdFilerReplicate,
  15. cmdServer,
  16. cmdMaster,
  17. cmdFiler,
  18. cmdS3,
  19. cmdUpload,
  20. cmdDownload,
  21. cmdScaffold,
  22. cmdShell,
  23. cmdVersion,
  24. cmdVolume,
  25. cmdExport,
  26. cmdMount,
  27. cmdWebDav,
  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. }