This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
subcommands.go
134 lines (108 loc) · 2.94 KB
/
subcommands.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package marvin
import (
"sort"
"strings"
"sync"
)
type subCommandWithHelp struct {
f SubCommandFunc
help string
}
func (sc subCommandWithHelp) Handle(t Team, args *CommandArguments) CommandResult {
return sc.f(t, args)
}
func (sc subCommandWithHelp) Help(t Team, args *CommandArguments) CommandResult {
return CmdHelpf(args, sc.help)
}
type ParentCommand struct {
extraHelp string
lock sync.Mutex
nameMap map[string]SubCommand
}
func NewParentCommand() *ParentCommand {
return &ParentCommand{
nameMap: make(map[string]SubCommand),
}
}
func (pc *ParentCommand) WithHelp(extraHelp string) *ParentCommand {
pc.extraHelp = extraHelp
return pc
}
func (pc *ParentCommand) RegisterCommand(name string, c SubCommand) {
pc.lock.Lock()
defer pc.lock.Unlock()
pc.nameMap[name] = c
}
func (pc *ParentCommand) RegisterCommandFunc(name string, f SubCommandFunc, help string) SubCommand {
pc.lock.Lock()
defer pc.lock.Unlock()
sc := subCommandWithHelp{f: f, help: help}
pc.nameMap[name] = sc
return sc
}
func (pc *ParentCommand) UnregisterCommand(name string) {
pc.lock.Lock()
defer pc.lock.Unlock()
delete(pc.nameMap, name)
}
func (pc *ParentCommand) Help(t Team, args *CommandArguments) CommandResult {
if len(args.Arguments) == 0 {
return pc.helpListCommands(t, args)
}
args.Command = args.Pop()
pc.lock.Lock()
subC, ok := pc.nameMap[args.Command]
pc.lock.Unlock()
if !ok {
cmdErr := CmdFailuref(args, "help: No such command `%s`", strings.Join(args.PreArgs(), " "))
cmdErr.Code = CmdResultNoSuchCommand
return cmdErr
}
return subC.Help(t, args)
}
func (pc *ParentCommand) helpListCommands(t Team, args *CommandArguments) CommandResult {
var subNames []string
for k := range pc.nameMap {
subNames = append(subNames, k)
}
sort.Strings(subNames)
preArgs := args.PreArgs()
if len(preArgs) > 1 {
if pc.extraHelp != "" {
return CmdHelpf(args, "%s\nSubcommands: `%s`", pc.extraHelp, strings.Join(subNames, "` `"))
}
return CmdHelpf(args, "Subcommands of `%s`:\n`%s`", strings.Join(preArgs[1:], " "), strings.Join(subNames, "` `"))
}
return CmdHelpf(args, "Available commands:\n`%s`", strings.Join(subNames, "` `"))
}
func (pc *ParentCommand) Handle(t Team, args *CommandArguments) CommandResult {
if len(args.Arguments) == 0 {
return pc.helpListCommands(t, args)
}
args.Command = args.Pop()
if args.Command == "help" {
if len(args.Arguments) == 0 {
return pc.Help(t, args)
} else {
args.Command = args.Pop()
pc.lock.Lock()
subC, ok := pc.nameMap[args.Command]
pc.lock.Unlock()
if !ok {
cmdErr := CmdFailuref(args, "help: No such command '%s'", args.Command)
cmdErr.Code = CmdResultNoSuchCommand
return cmdErr
}
return subC.Help(t, args)
}
}
pc.lock.Lock()
subC, ok := pc.nameMap[args.Command]
pc.lock.Unlock()
if !ok {
cmdErr := CmdFailuref(args, "No such subcommand '%s'", args.Command)
cmdErr.Code = CmdResultNoSuchCommand
return cmdErr
}
return subC.Handle(t, args)
}