Skip to content

Commit 69147fe

Browse files
committed
Improve fuzzy search and fix test
1 parent 80d2df9 commit 69147fe

File tree

3 files changed

+6
-21
lines changed

3 files changed

+6
-21
lines changed

cf_cli_java_plugin.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ import (
2222

2323
"utils"
2424

25-
"github.com/lithammer/fuzzysearch/fuzzy"
2625
guuid "github.com/satori/go.uuid"
2726
"github.com/simonleung8/flags"
28-
"sort"
2927
)
3028

3129
// The JavaPlugin is a cf cli plugin that supports taking heap and thread dumps on demand
@@ -414,18 +412,8 @@ func (c *JavaPlugin) execute(commandExecutor cmd.CommandExecutor, uuidGenerator
414412
for _, command := range commands {
415413
avCommands = append(avCommands, command.Name)
416414
}
417-
matches := fuzzy.RankFind(commandName, avCommands)
418-
sort.Sort(matches)
419-
matchedCommands := make([]string, 0, 3)
420-
// first three matches
421-
for i := 0; i < 3 && i < len(matches); i++ {
422-
matchedCommands = append(matchedCommands, matches[i].Target)
423-
}
424-
// concat with or at the end
425-
if len(matchedCommands) > 1 {
426-
matchedCommands[len(matchedCommands)-1] = "or " + matchedCommands[len(matchedCommands)-1]
427-
}
428-
return "", &InvalidUsageError{message: fmt.Sprintf("Unrecognized command %q, did you mean: %s?", commandName, strings.Join(matchedCommands, ", "))}
415+
matches := utils.FuzzySearch(commandName, avCommands, 3)
416+
return "", &InvalidUsageError{message: fmt.Sprintf("Unrecognized command %q, did you mean: %s?", commandName, utils.JoinWithOr(matches))}
429417
}
430418

431419
command := commands[index]

cf_cli_java_plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ var _ = Describe("CfJavaPlugin", func() {
117117
})
118118

119119
Expect(output).To(BeEmpty())
120-
Expect(err.Error()).To(ContainSubstring("Unrecognized command \"UNKNOWN_COMMAND\": supported commands"))
121-
Expect(cliOutput).To(ContainSubstring("Unrecognized command \"UNKNOWN_COMMAND\": supported commands"))
120+
Expect(err.Error()).To(ContainSubstring("Unrecognized command \"UNKNOWN_COMMAND\", did you mean:"))
121+
Expect(cliOutput).To(ContainSubstring("Unrecognized command \"UNKNOWN_COMMAND\", did you mean:"))
122122

123123
Expect(commandExecutor.ExecuteCallCount()).To(Equal(1))
124124
Expect(commandExecutor.ExecuteArgsForCall(0)).To(Equal([]string{"help", "java"}))

utils/cfutils.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"os/exec"
99
"strings"
1010
"slices"
11-
"sort"
12-
"github.com/lithammer/fuzzysearch/fuzzy"
1311
)
1412

1513
type CfJavaPluginUtilImpl struct {
@@ -118,9 +116,8 @@ func (checker CfJavaPluginUtilImpl) FindReasonForAccessError(app string) string
118116
if slices.Contains(appNames, app) {
119117
return "Problems accessing the app " + app
120118
}
121-
matches := fuzzy.RankFind(app, appNames)
122-
sort.Sort(matches)
123-
return "Could not find " + app + ". Did you mean " + matches[0].Target + "?"
119+
matches := FuzzySearch(app, appNames, 1)
120+
return "Could not find " + app + ". Did you mean " + matches[0] + "?"
124121
}
125122
func (checker CfJavaPluginUtilImpl) CheckRequiredTools(app string) (bool, error) {
126123
guid, err := exec.Command("cf", "app", app, "--guid").Output()

0 commit comments

Comments
 (0)