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