feat(help): show suggestions for missing topics
All checks were successful
Security Scan / security (push) Successful in 20s
All checks were successful
Security Scan / security (push) Successful in 20s
This commit is contained in:
parent
2a9177a30b
commit
f376372630
2 changed files with 32 additions and 0 deletions
|
|
@ -33,6 +33,12 @@ func AddHelpCommands(root *cli.Command) {
|
|||
|
||||
topic, err := catalog.Get(args[0])
|
||||
if err != nil {
|
||||
if suggestions := catalog.Search(args[0]); len(suggestions) > 0 {
|
||||
if suggestErr := renderSearchResults(suggestions, args[0]); suggestErr != nil {
|
||||
return suggestErr
|
||||
}
|
||||
cli.Blank()
|
||||
}
|
||||
return cli.Err("help topic %q not found", args[0])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -135,4 +135,30 @@ func TestAddHelpCommands_Bad(t *testing.T) {
|
|||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "help topic")
|
||||
})
|
||||
|
||||
t.Run("missing topic shows suggestions when available", func(t *testing.T) {
|
||||
catalog := gohelp.DefaultCatalog()
|
||||
query := ""
|
||||
for _, candidate := range []string{"configuration", "docs", "search", "topic", "help"} {
|
||||
if _, err := catalog.Get(candidate); err == nil {
|
||||
continue
|
||||
}
|
||||
if len(catalog.Search(candidate)) > 0 {
|
||||
query = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
if query == "" {
|
||||
t.Skip("no suitable query found with suggestions")
|
||||
}
|
||||
|
||||
cmd := newHelpCommand(t)
|
||||
out := captureOutput(t, func() {
|
||||
err := cmd.RunE(cmd, []string{query})
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "help topic")
|
||||
})
|
||||
|
||||
assert.Contains(t, out, "SEARCH RESULTS")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue