Skip to content

Commit

Permalink
add helper for inferring index name from input
Browse files Browse the repository at this point in the history
To be used in multi-index work.

Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb committed Feb 21, 2020
1 parent fb5343a commit ca81ca7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"strings"

"github.com/pkg/errors"

"sigs.k8s.io/krew/pkg/constants"
)

// IsSubPath checks if the extending path is an extension of the basePath, it will return the extending path
Expand All @@ -42,3 +44,13 @@ func ReplaceBase(path, old, replacement string) (string, error) {
}
return filepath.Join(replacement, extendingPath), nil
}

// CanonicalPluginName resolves a plugin's index and name from input string.
// If an index is not specified, the default index name is assumed.
func CanonicalPluginName(in string) (string, string) {
if strings.Count(in, "/") == 0 {
return constants.DefaultIndexName, in
}
p := strings.SplitN(in, "/", 2)
return p[0], p[1]
}
24 changes: 24 additions & 0 deletions internal/pathutil/pathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,27 @@ func TestReplaceBase(t *testing.T) {
})
}
}

func TestCanonicalPluginName(t *testing.T) {
tests := []struct {
in string
wantIndex string
wantName string
}{
{"foo", "default", "foo"},
{"", "default", ""}, // despite unsupported
{"a/b", "a", "b"},
{"a/b/c", "a", "b/c"}, // despite unsupported
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
gotIndex, gotName := CanonicalPluginName(tt.in)
if gotIndex != tt.wantIndex {
t.Errorf("CanonicalPluginName(%q) gotIndex = %q, want = %q", tt.in, gotIndex, tt.wantIndex)
}
if gotName != tt.wantName {
t.Errorf("CanonicalPluginName(%q) gotName = %q, want = %q", tt.in, gotName, tt.wantName)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ const (

// IndexURI points to the upstream index.
IndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
// DefaultIndexName is a magic string that's used for a plugin name specified without an index.
DefaultIndexName = "default"
)

0 comments on commit ca81ca7

Please sign in to comment.