command.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package sshd
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "github.com/armon/go-radix"
  9. )
  10. // CommandFlags is a function called before help or command execution to parse command line flags
  11. // It should return a flag.FlagSet instance and a pointer to the struct that will contain parsed flags
  12. type CommandFlags func() (*flag.FlagSet, interface{})
  13. // CommandCallback is the function called when your command should execute.
  14. // fs will be a a pointer to the struct provided by Command.Flags callback, if there was one. -h and -help are reserved
  15. // and handled automatically for you.
  16. // a will be any unconsumed arguments, if no Command.Flags was available this will be all the flags passed in.
  17. // w is the writer to use when sending messages back to the client.
  18. // If an error is returned by the callback it is logged locally, the callback should handle messaging errors to the user
  19. // where appropriate
  20. type CommandCallback func(fs interface{}, a []string, w StringWriter) error
  21. type Command struct {
  22. Name string
  23. ShortDescription string
  24. Help string
  25. Flags CommandFlags
  26. Callback CommandCallback
  27. }
  28. func execCommand(c *Command, args []string, w StringWriter) error {
  29. var (
  30. fl *flag.FlagSet
  31. fs interface{}
  32. )
  33. if c.Flags != nil {
  34. fl, fs = c.Flags()
  35. if fl != nil {
  36. // SetOutput() here in case fl.Parse dumps usage.
  37. fl.SetOutput(w.GetWriter())
  38. err := fl.Parse(args)
  39. if err != nil {
  40. // fl.Parse has dumped error information to the user via the w writer.
  41. return err
  42. }
  43. args = fl.Args()
  44. }
  45. }
  46. return c.Callback(fs, args, w)
  47. }
  48. func dumpCommands(c *radix.Tree, w StringWriter) {
  49. err := w.WriteLine("Available commands:")
  50. if err != nil {
  51. //TODO: log
  52. return
  53. }
  54. cmds := make([]string, 0)
  55. for _, l := range allCommands(c) {
  56. cmds = append(cmds, fmt.Sprintf("%s - %s", l.Name, l.ShortDescription))
  57. }
  58. sort.Strings(cmds)
  59. err = w.Write(strings.Join(cmds, "\n") + "\n\n")
  60. if err != nil {
  61. //TODO: log
  62. }
  63. }
  64. func lookupCommand(c *radix.Tree, sCmd string) (*Command, error) {
  65. cmd, ok := c.Get(sCmd)
  66. if !ok {
  67. return nil, nil
  68. }
  69. command, ok := cmd.(*Command)
  70. if !ok {
  71. return nil, errors.New("failed to cast command")
  72. }
  73. return command, nil
  74. }
  75. func matchCommand(c *radix.Tree, cmd string) []string {
  76. cmds := make([]string, 0)
  77. c.WalkPrefix(cmd, func(found string, v interface{}) bool {
  78. cmds = append(cmds, found)
  79. return false
  80. })
  81. sort.Strings(cmds)
  82. return cmds
  83. }
  84. func allCommands(c *radix.Tree) []*Command {
  85. cmds := make([]*Command, 0)
  86. c.WalkPrefix("", func(found string, v interface{}) bool {
  87. cmd, ok := v.(*Command)
  88. if ok {
  89. cmds = append(cmds, cmd)
  90. }
  91. return false
  92. })
  93. return cmds
  94. }
  95. func helpCallback(commands *radix.Tree, a []string, w StringWriter) (err error) {
  96. // Just typed help
  97. if len(a) == 0 {
  98. dumpCommands(commands, w)
  99. return nil
  100. }
  101. // We are printing a specific commands help text
  102. cmd, err := lookupCommand(commands, a[0])
  103. if err != nil {
  104. //TODO: handle error
  105. //TODO: message the user
  106. return
  107. }
  108. if cmd != nil {
  109. err = w.WriteLine(fmt.Sprintf("%s - %s", cmd.Name, cmd.ShortDescription))
  110. if err != nil {
  111. return err
  112. }
  113. if cmd.Help != "" {
  114. err = w.WriteLine(fmt.Sprintf(" %s", cmd.Help))
  115. if err != nil {
  116. return err
  117. }
  118. }
  119. if cmd.Flags != nil {
  120. fs, _ := cmd.Flags()
  121. if fs != nil {
  122. fs.SetOutput(w.GetWriter())
  123. fs.PrintDefaults()
  124. }
  125. }
  126. return nil
  127. }
  128. err = w.WriteLine("Command not available " + a[0])
  129. if err != nil {
  130. return err
  131. }
  132. return nil
  133. }
  134. func checkHelpArgs(args []string) bool {
  135. for _, a := range args {
  136. if a == "-h" || a == "-help" {
  137. return true
  138. }
  139. }
  140. return false
  141. }