Skip to content

Commit

Permalink
Update each index instead of just default
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskim06 committed Apr 6, 2020
1 parent 8cfa5d2 commit 702d3a5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 37 deletions.
26 changes: 26 additions & 0 deletions cmd/krew/cmd/namingutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
package cmd

import (
"os"

"github.com/pkg/errors"

"sigs.k8s.io/krew/internal/environment"
"sigs.k8s.io/krew/internal/index/indexoperations"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)
Expand Down Expand Up @@ -48,3 +54,23 @@ func canonicalName(p index.Plugin, indexName string) string {
}
return indexName + "/" + p.Name
}

// allIndexes returns a slice of index name and URL pairs
func allIndexes(p environment.Paths) ([]indexoperations.Index, error) {
indexes := []indexoperations.Index{
{
Name: constants.DefaultIndexName,
URL: constants.DefaultIndexURI,
},
}
if os.Getenv(constants.EnableMultiIndexSwitch) != "" {
out, err := indexoperations.ListIndexes(p)
if err != nil {
return nil, errors.Wrapf(err, "failed to list plugin indexes available")
}
if len(out) != 0 {
indexes = out
}
}
return indexes, nil
}
50 changes: 50 additions & 0 deletions cmd/krew/cmd/namingutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package cmd

import (
"os"
"testing"

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

"sigs.k8s.io/krew/internal/environment"
"sigs.k8s.io/krew/internal/index/indexoperations"
"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
Expand Down Expand Up @@ -112,3 +115,50 @@ func Test_canonicalName(t *testing.T) {
t.Errorf("expected=%q; got=%q", expected, got)
}
}

func Test_allIndexes(t *testing.T) {
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()
paths := environment.NewPaths(tmpDir.Root())

expected := []indexoperations.Index{
{
Name: constants.DefaultIndexName,
URL: constants.DefaultIndexURI,
},
}
actual, err := allIndexes(paths)
if err != nil {
t.Errorf("unexpected error getting indexes: %s", err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("got diffent output: %s", diff)
}

os.Setenv(constants.EnableMultiIndexSwitch, "1")
defer os.Unsetenv(constants.EnableMultiIndexSwitch)

expected = []indexoperations.Index{
{
Name: "custom",
URL: "https://github.com/custom/index.git",
},
{
Name: "foo",
URL: "https://github.com/foo/index.git",
},
}

for _, index := range expected {
path := paths.IndexPath(index.Name)
tmpDir.InitEmptyGitRepo(path, index.URL)
}

actual, err = allIndexes(paths)
if err != nil {
t.Errorf("unexpected error getting indexes: %s", err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("got diffent output: %s", diff)
}
}
17 changes: 3 additions & 14 deletions cmd/krew/cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import (
"github.com/spf13/cobra"
"k8s.io/klog"

"sigs.k8s.io/krew/internal/index/indexoperations"
"sigs.k8s.io/krew/internal/index/indexscanner"
"sigs.k8s.io/krew/internal/installation"
"sigs.k8s.io/krew/pkg/constants"
)

// searchCmd represents the search command
Expand All @@ -44,18 +42,9 @@ Examples:
To fuzzy search plugins with a keyword:
kubectl krew search KEYWORD`,
RunE: func(cmd *cobra.Command, args []string) error {
indexes := []indexoperations.Index{
{
Name: constants.DefaultIndexName,
URL: constants.IndexURI, // unused here but providing for completeness
},
}
if os.Getenv(constants.EnableMultiIndexSwitch) != "" {
out, err := indexoperations.ListIndexes(paths)
if err != nil {
return errors.Wrapf(err, "failed to list plugin indexes available")
}
indexes = out
indexes, err := allIndexes(paths)
if err != nil {
return err
}

klog.V(3).Infof("found %d indexes", len(indexes))
Expand Down
13 changes: 10 additions & 3 deletions cmd/krew/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,16 @@ func showUpdatedPlugins(out io.Writer, preUpdate, posUpdate []index.Plugin, inst
func ensureIndexUpdated(_ *cobra.Command, _ []string) error {
preUpdateIndex, _ := indexscanner.LoadPluginListFromFS(paths.IndexPluginsPath(constants.DefaultIndexName))

klog.V(1).Infof("Updating the local copy of plugin index (%s)", paths.IndexPath(constants.DefaultIndexName))
if err := gitutil.EnsureUpdated(constants.DefaultIndexURI, paths.IndexPath(constants.DefaultIndexName)); err != nil {
return errors.Wrap(err, "failed to update the local index")
indexes, err := allIndexes(paths)
if err != nil {
return err
}
for _, idx := range indexes {
indexPath := paths.IndexPath(idx.Name)
klog.V(1).Infof("Updating the local copy of plugin index (%s)", indexPath)
if err := gitutil.EnsureUpdated(idx.URL, indexPath); err != nil {
return errors.Wrapf(err, "failed to update the local index %s", idx.Name)
}
}
fmt.Fprintln(os.Stderr, "Updated the local copy of plugin index.")

Expand Down
2 changes: 1 addition & 1 deletion integration_test/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestKrewIndexAddUnsafe(t *testing.T) {
expected := "invalid index name"

for _, c := range cases {
b, err := test.Krew("index", "add", c, constants.IndexURI).Run()
b, err := test.Krew("index", "add", c, constants.DefaultIndexURI).Run()
if err == nil {
t.Fatalf("%q: expected error", c)
} else if !strings.Contains(string(b), expected) {
Expand Down
23 changes: 4 additions & 19 deletions internal/index/indexoperations/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/google/go-cmp/cmp"

"sigs.k8s.io/krew/internal/environment"
"sigs.k8s.io/krew/internal/gitutil"
"sigs.k8s.io/krew/internal/testutil"
"sigs.k8s.io/krew/pkg/constants"
)
Expand All @@ -49,7 +48,7 @@ func TestListIndexes(t *testing.T) {
paths := environment.NewPaths(tmpDir.Root())
for _, index := range wantIndexes {
path := paths.IndexPath(index.Name)
initEmptyGitRepo(t, path, index.URL)
tmpDir.InitEmptyGitRepo(path, index.URL)
}

gotIndexes, err := ListIndexes(paths)
Expand All @@ -71,7 +70,7 @@ func TestAddIndexSuccess(t *testing.T) {

indexName := "foo"
localRepo := tmpDir.Path("local/" + indexName)
initEmptyGitRepo(t, localRepo, "")
tmpDir.InitEmptyGitRepo(localRepo, "")

paths := environment.NewPaths(tmpDir.Root())
if err := AddIndex(paths, indexName, localRepo); err != nil {
Expand Down Expand Up @@ -107,8 +106,8 @@ func TestAddIndexFailure(t *testing.T) {
}

localRepo := tmpDir.Path("local/" + indexName)
initEmptyGitRepo(t, tmpDir.Path("index/"+indexName), "")
initEmptyGitRepo(t, localRepo, "")
tmpDir.InitEmptyGitRepo(tmpDir.Path("index/"+indexName), "")
tmpDir.InitEmptyGitRepo(localRepo, "")

if err := AddIndex(paths, indexName, localRepo); err == nil {
t.Error("expected error when adding an index that already exists")
Expand Down Expand Up @@ -151,20 +150,6 @@ func TestDeleteIndex(t *testing.T) {
}
}

func initEmptyGitRepo(t *testing.T, path, url string) {
t.Helper()

if err := os.MkdirAll(path, os.ModePerm); err != nil {
t.Fatalf("cannot create directory %q: %s", filepath.Dir(path), err)
}
if _, err := gitutil.Exec(path, "init"); err != nil {
t.Fatalf("error initializing git repo: %s", err)
}
if _, err := gitutil.Exec(path, "remote", "add", "origin", url); err != nil {
t.Fatalf("error setting remote origin: %s", err)
}
}

func TestIsValidIndexName(t *testing.T) {
tests := []struct {
name string
Expand Down
15 changes: 15 additions & 0 deletions internal/testutil/tempdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"testing"

"sigs.k8s.io/krew/internal/gitutil"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -83,3 +84,17 @@ func (td *TempDir) WriteYAML(file string, obj interface{}) *TempDir {
}
return td.Write(file, content)
}

func (td *TempDir) InitEmptyGitRepo(path, url string) {
td.t.Helper()

if err := os.MkdirAll(path, os.ModePerm); err != nil {
td.t.Fatalf("cannot create directory %q: %s", filepath.Dir(path), err)
}
if _, err := gitutil.Exec(path, "init"); err != nil {
td.t.Fatalf("error initializing git repo: %s", err)
}
if _, err := gitutil.Exec(path, "remote", "add", "origin", url); err != nil {
td.t.Fatalf("error setting remote origin: %s", err)
}
}

0 comments on commit 702d3a5

Please sign in to comment.