command.go 3.5 KB

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