command.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package sshd
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "github.com/armon/go-radix"
  7. "sort"
  8. "strings"
  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. //TODO: handle the error
  37. fl.Parse(args)
  38. args = fl.Args()
  39. }
  40. }
  41. return c.Callback(fs, args, w)
  42. }
  43. func dumpCommands(c *radix.Tree, w StringWriter) {
  44. err := w.WriteLine("Available commands:")
  45. if err != nil {
  46. //TODO: log
  47. return
  48. }
  49. cmds := make([]string, 0)
  50. for _, l := range allCommands(c) {
  51. cmds = append(cmds, fmt.Sprintf("%s - %s", l.Name, l.ShortDescription))
  52. }
  53. sort.Strings(cmds)
  54. err = w.Write(strings.Join(cmds, "\n") + "\n\n")
  55. if err != nil {
  56. //TODO: log
  57. }
  58. }
  59. func lookupCommand(c *radix.Tree, sCmd string) (*Command, error) {
  60. cmd, ok := c.Get(sCmd)
  61. if !ok {
  62. return nil, nil
  63. }
  64. command, ok := cmd.(*Command)
  65. if !ok {
  66. return nil, errors.New("failed to cast command")
  67. }
  68. return command, nil
  69. }
  70. func matchCommand(c *radix.Tree, cmd string) []string {
  71. cmds := make([]string, 0)
  72. c.WalkPrefix(cmd, func(found string, v interface{}) bool {
  73. cmds = append(cmds, found)
  74. return false
  75. })
  76. sort.Strings(cmds)
  77. return cmds
  78. }
  79. func allCommands(c *radix.Tree) []*Command {
  80. cmds := make([]*Command, 0)
  81. c.WalkPrefix("", func(found string, v interface{}) bool {
  82. cmd, ok := v.(*Command)
  83. if ok {
  84. cmds = append(cmds, cmd)
  85. }
  86. return false
  87. })
  88. return cmds
  89. }
  90. func helpCallback(commands *radix.Tree, a []string, w StringWriter) (err error) {
  91. // Just typed help
  92. if len(a) == 0 {
  93. dumpCommands(commands, w)
  94. return nil
  95. }
  96. // We are printing a specific commands help text
  97. cmd, err := lookupCommand(commands, a[0])
  98. if err != nil {
  99. //TODO: handle error
  100. //TODO: message the user
  101. return
  102. }
  103. if cmd != nil {
  104. err = w.WriteLine(fmt.Sprintf("%s - %s", cmd.Name, cmd.ShortDescription))
  105. if err != nil {
  106. return err
  107. }
  108. if cmd.Help != "" {
  109. err = w.WriteLine(fmt.Sprintf(" %s", cmd.Help))
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. if cmd.Flags != nil {
  115. fs, _ := cmd.Flags()
  116. if fs != nil {
  117. fs.SetOutput(w.GetWriter())
  118. fs.PrintDefaults()
  119. }
  120. }
  121. return nil
  122. }
  123. err = w.WriteLine("Command not available " + a[0])
  124. if err != nil {
  125. return err
  126. }
  127. return nil
  128. }
  129. func checkHelpArgs(args []string) bool {
  130. for _, a := range args {
  131. if a == "-h" || a == "-help" {
  132. return true
  133. }
  134. }
  135. return false
  136. }