Skip to content

Commit b8666fe

Browse files
committed
Allow retrieving sub commands usage via help args
1 parent 9590975 commit b8666fe

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

pkg/bot/command.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,40 @@ func (dc *RootCommand) Execute(ctx domain.Context) error {
182182
return c.Execute(ctx)
183183
}
184184

185+
func (dc *RootCommand) HasSubcommands() bool {
186+
return len(dc.cmds) > 0
187+
}
188+
189+
func (dc *RootCommand) GetSubcommand(verb string) (domain.Command, bool) {
190+
c, ok := dc.cmds[verb]
191+
return c, ok
192+
}
193+
194+
func (dc *RootCommand) getMatchingCommand(args []string) (domain.Command, bool) {
195+
cur, ok := dc.GetSubcommand(args[0])
196+
if !ok {
197+
return nil, false
198+
}
199+
for _, arg := range args[1:] {
200+
cur, ok = cur.GetSubcommand(arg)
201+
if !ok {
202+
return nil, false
203+
}
204+
}
205+
return cur, true
206+
}
207+
208+
func (dc *RootCommand) HelpMessage(_ int, _ bool) []string {
209+
var lines []string
210+
names := lo.Keys(dc.cmds)
211+
slices.Sort(names)
212+
for _, name := range names {
213+
cmd := dc.cmds[name]
214+
lines = append(lines, cmd.HelpMessage(0, false)...)
215+
}
216+
return lines
217+
}
218+
185219
func (c *CommandInstance) Execute(ctx domain.Context) error {
186220
// If this command has permitted operators defined, check operator
187221
if len(c.operators) > 0 {
@@ -268,15 +302,13 @@ func (c *CommandInstance) Execute(ctx domain.Context) error {
268302
return ctx.ReplySuccess(replyMessage...)
269303
}
270304

271-
func (dc *RootCommand) HelpMessage(_ int, _ bool) []string {
272-
var lines []string
273-
names := lo.Keys(dc.cmds)
274-
slices.Sort(names)
275-
for _, name := range names {
276-
cmd := dc.cmds[name]
277-
lines = append(lines, cmd.HelpMessage(0, false)...)
278-
}
279-
return lines
305+
func (c *CommandInstance) HasSubcommands() bool {
306+
return len(c.subCommands) > 0
307+
}
308+
309+
func (c *CommandInstance) GetSubcommand(verb string) (domain.Command, bool) {
310+
sub, ok := c.subCommands[verb]
311+
return sub, ok
280312
}
281313

282314
func (c *CommandInstance) HelpMessage(indent int, formatSub bool) []string {

pkg/bot/help.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,43 @@ type HelpCommand struct {
1717

1818
func (h *HelpCommand) Execute(ctx domain.Context) error {
1919
var lines []string
20-
lines = append(lines, fmt.Sprintf("## DevOpsBot v%s", utils.Version()))
20+
args := ctx.Args()
21+
22+
// Root usage
23+
if len(args) == 0 {
24+
lines = append(lines, fmt.Sprintf("## DevOpsBot v%s", utils.Version()))
25+
lines = append(lines, "")
26+
lines = append(lines, h.root.HelpMessage(0, true)...)
27+
lines = append(lines, "")
28+
lines = append(lines, fmt.Sprintf("Type `%shelp command-name` for more help", config.C.Prefix))
29+
return ctx.ReplySuccess(lines...)
30+
}
31+
32+
// Specific command usage
33+
c, ok := h.root.getMatchingCommand(args)
34+
if !ok {
35+
lines = append(lines, fmt.Sprintf("Command `%s%s` not found, try `%shelp`?", config.C.Prefix, strings.Join(args, " "), config.C.Prefix))
36+
return ctx.ReplyBad(lines...)
37+
}
38+
39+
lines = append(lines, fmt.Sprintf("## `%s%s` Usage", config.C.Prefix, strings.Join(args, " ")))
2140
lines = append(lines, "")
22-
lines = append(lines, h.root.HelpMessage(0, true)...)
41+
lines = append(lines, c.HelpMessage(0, true)...)
42+
if c.HasSubcommands() {
43+
lines = append(lines, "")
44+
lines = append(lines, fmt.Sprintf("Type `%shelp command-name [sub-commands...]` for more help", config.C.Prefix))
45+
}
2346
return ctx.ReplySuccess(lines...)
2447
}
2548

49+
func (h *HelpCommand) HasSubcommands() bool {
50+
return false
51+
}
52+
53+
func (h *HelpCommand) GetSubcommand(_ string) (domain.Command, bool) {
54+
return nil, false
55+
}
56+
2657
func (h *HelpCommand) HelpMessage(indent int, _ bool) []string {
2758
return []string{fmt.Sprintf(
2859
"%s- `%shelp` - Display help message.",

pkg/domain/bot.go

+2
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,7 @@ type Context interface {
5151
// Command コマンドインターフェース
5252
type Command interface {
5353
Execute(ctx Context) error
54+
HasSubcommands() bool
55+
GetSubcommand(verb string) (Command, bool)
5456
HelpMessage(indent int, formatSub bool) []string
5557
}

0 commit comments

Comments
 (0)