forked from cloudfoundry/cli
/
unknown_command_error.go
45 lines (35 loc) · 1.08 KB
/
unknown_command_error.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
package command_parser
import (
"fmt"
"reflect"
"code.cloudfoundry.org/cli/cf/util/spellcheck"
"code.cloudfoundry.org/cli/command/common"
)
type UnknownCommandError struct {
CommandName string
suggestions []string
}
func (e *UnknownCommandError) Suggest(pluginCommandNames []string) {
var commandNames []string
commandListStruct := reflect.TypeOf(common.Commands)
commandListStruct.FieldByNameFunc(
func(fieldName string) bool {
field, _ := commandListStruct.FieldByName(fieldName)
if commandName := field.Tag.Get("command"); commandName != "" {
commandNames = append(commandNames, commandName)
}
return false
})
cmdSuggester := spellcheck.NewCommandSuggester(append(commandNames, pluginCommandNames...))
e.suggestions = cmdSuggester.Recommend(e.CommandName)
}
func (e UnknownCommandError) Error() string {
message := fmt.Sprintf("'%s' is not a registered command. See 'cf help -a'", e.CommandName)
if len(e.suggestions) > 0 {
message += "\n\nDid you mean?"
for _, suggestion := range e.suggestions {
message += "\n " + suggestion
}
}
return message
}