-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathhelp.go
97 lines (72 loc) · 1.75 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package commands
import (
"fmt"
"io"
"sort"
"github.com/NHAS/reverse_ssh/internal/server/users"
"github.com/NHAS/reverse_ssh/internal/terminal"
"github.com/NHAS/reverse_ssh/internal/terminal/autocomplete"
"github.com/NHAS/reverse_ssh/pkg/table"
)
type help struct {
}
func (h *help) ValidArgs() map[string]string {
return map[string]string{"l": "List all function names only"}
}
func (h *help) Run(user *users.User, tty io.ReadWriter, line terminal.ParsedLine) error {
if line.IsSet("l") {
funcs := []string{}
for funcName := range allCommands {
funcs = append(funcs, funcName)
}
sort.Strings(funcs)
for _, funcName := range funcs {
fmt.Fprintln(tty, funcName)
}
return nil
}
if len(line.Arguments) < 1 {
t, err := table.NewTable("Commands", "Function", "Purpose")
if err != nil {
return err
}
keys := []string{}
for funcName := range allCommands {
keys = append(keys, funcName)
}
sort.Strings(keys)
for _, k := range keys {
hf := allCommands[k].Help
err = t.AddValues(k, hf(true))
if err != nil {
return err
}
}
t.Fprint(tty)
return nil
}
l, ok := allCommands[line.Arguments[0].Value()]
if !ok {
return fmt.Errorf("Command %s not found", line.Arguments[0].Value())
}
fmt.Fprintf(tty, "\ndescription:\n%s\n", l.Help(true))
fmt.Fprintf(tty, "\nusage:\n%s\n", l.Help(false))
return nil
}
func (h *help) Expect(line terminal.ParsedLine) []string {
if len(line.Arguments) <= 1 {
return []string{autocomplete.Functions}
}
return nil
}
func (h *help) Help(explain bool) string {
const description = "Get help for commands, or display all commands"
if explain {
return description
}
return terminal.MakeHelpText(h.ValidArgs(),
"help",
"help <functions>",
description,
)
}