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 <ahmetb@google.com>
  • Loading branch information
ahmetb committed Feb 21, 2020
1 parent fb5343a commit d26446b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/pathutil/pathutil.go
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) {
p := strings.SplitN(in, "/", 2)
if strings.Count(in, "/") == 0 {
return constants.DefaultIndexName, in
}
return p[0], p[1]
}
24 changes: 24 additions & 0 deletions internal/pathutil/pathutil_test.go
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)
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/constants/constants.go
Expand Up @@ -21,5 +21,6 @@ const (
KrewPluginName = "krew" // plugin name of krew itself

// IndexURI points to the upstream index.
IndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
IndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
DefaultIndexName = "default"
)

0 comments on commit d26446b

Please sign in to comment.