Skip to content

Commit

Permalink
Support search plugins by name and description
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw99 committed Oct 19, 2022
1 parent f9772a9 commit bdb1f3c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*.so
*.dylib

# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
.idea/

# Test binary, build with `go test -c`
*.test

Expand Down
39 changes: 30 additions & 9 deletions cmd/krew/cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ Examples:
}

pluginCanonicalNames := make([]string, len(plugins))
pluginDescriptions := make([]string, len(plugins))
pluginCanonicalNameMap := make(map[string]pluginEntry, len(plugins))
for i, p := range plugins {
cn := canonicalName(p.p, p.indexName)
pluginCanonicalNames[i] = cn
pluginDescriptions[i] = p.p.Spec.ShortDescription
pluginCanonicalNameMap[cn] = p
}

Expand All @@ -80,15 +82,8 @@ Examples:
installed[cn] = true
}

var searchResults []string
if len(args) > 0 {
matches := fuzzy.Find(strings.Join(args, ""), pluginCanonicalNames)
for _, m := range matches {
searchResults = append(searchResults, m.Str)
}
} else {
searchResults = pluginCanonicalNames
}
keyword := strings.Join(args, "")
searchResults := searchByNameAndDesc(keyword, pluginCanonicalNames, pluginDescriptions)

// No plugins found
if len(searchResults) == 0 {
Expand Down Expand Up @@ -118,6 +113,32 @@ Examples:
PreRunE: checkIndex,
}

func searchByNameAndDesc(keyword string, names, descs []string) []string {
var searchResults []string
if keyword == "" {
return names
}

// find by names
matches := fuzzy.Find(keyword, names)
searchResultsMap := make(map[int]bool)
for _, m := range matches {
searchResults = append(searchResults, m.Str)
searchResultsMap[m.Index] = true
}

// find by short descriptions
fuzzyMatches := fuzzy.Find(keyword, descs)
for _, m := range fuzzyMatches {
// use map to deduplicate and score > 0 to match more accurately
if _, exist := searchResultsMap[m.Index]; !exist && m.Score > 0 {
searchResults = append(searchResults, names[m.Index])
}
}

return searchResults
}

func limitString(s string, length int) string {
if len(s) > length && length > 3 {
s = s[:length-3] + "..."
Expand Down
58 changes: 58 additions & 0 deletions cmd/krew/cmd/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func Test_searchByNameAndDesc(t *testing.T) {
keyword := "config"
testPlugins := []struct {
names []string
descs []string
expected []string
}{
{
names: []string{"switch-config", "ctx", "kc"},
descs: []string{
" Switches between kubeconfig files",
"Switch between contexts in your kubeconfig",
"Interactive CRUD operations to manage kubeconfig",
},
expected: []string{"switch-config", "ctx", "kc"},
},
{
names: []string{"ns", "ctx", "kc"},
descs: []string{
"Switch between Kubernetes namespaces",
"Switch between contexts in your kubeconfig",
"Interactive CRUD operations to manage kubeconfig",
},
expected: []string{"ctx", "kc"},
},
}

for _, tp := range testPlugins {
t.Run(tp.names[0], func(t *testing.T) {
result := searchByNameAndDesc(keyword, tp.names, tp.descs)
if diff := cmp.Diff(tp.expected, result); diff != "" {
t.Fatalf("expected %v does not match got %v", tp.expected, result)
}
})
}
}

0 comments on commit bdb1f3c

Please sign in to comment.