From 702d3a5f41be76a21faa65ce4b157493b695ab69 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Mon, 6 Apr 2020 11:51:12 -0700 Subject: [PATCH] Update each index instead of just default --- cmd/krew/cmd/namingutils.go | 26 ++++++++++ cmd/krew/cmd/namingutils_test.go | 50 ++++++++++++++++++++ cmd/krew/cmd/search.go | 17 ++----- cmd/krew/cmd/update.go | 13 +++-- integration_test/index_test.go | 2 +- internal/index/indexoperations/index_test.go | 23 ++------- internal/testutil/tempdir.go | 15 ++++++ 7 files changed, 109 insertions(+), 37 deletions(-) diff --git a/cmd/krew/cmd/namingutils.go b/cmd/krew/cmd/namingutils.go index f73a9804..a1a8dd03 100644 --- a/cmd/krew/cmd/namingutils.go +++ b/cmd/krew/cmd/namingutils.go @@ -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" ) @@ -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 +} diff --git a/cmd/krew/cmd/namingutils_test.go b/cmd/krew/cmd/namingutils_test.go index f8a508f4..e1b41c51 100644 --- a/cmd/krew/cmd/namingutils_test.go +++ b/cmd/krew/cmd/namingutils_test.go @@ -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" @@ -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) + } +} diff --git a/cmd/krew/cmd/search.go b/cmd/krew/cmd/search.go index 313f1c42..9ac0c169 100644 --- a/cmd/krew/cmd/search.go +++ b/cmd/krew/cmd/search.go @@ -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 @@ -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)) diff --git a/cmd/krew/cmd/update.go b/cmd/krew/cmd/update.go index 12ca904e..30eafe52 100644 --- a/cmd/krew/cmd/update.go +++ b/cmd/krew/cmd/update.go @@ -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.") diff --git a/integration_test/index_test.go b/integration_test/index_test.go index 693cffd1..6264bb49 100644 --- a/integration_test/index_test.go +++ b/integration_test/index_test.go @@ -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) { diff --git a/internal/index/indexoperations/index_test.go b/internal/index/indexoperations/index_test.go index ea44bf92..c8ac1457 100644 --- a/internal/index/indexoperations/index_test.go +++ b/internal/index/indexoperations/index_test.go @@ -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" ) @@ -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) @@ -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 { @@ -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") @@ -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 diff --git a/internal/testutil/tempdir.go b/internal/testutil/tempdir.go index 9b28feea..479877f1 100644 --- a/internal/testutil/tempdir.go +++ b/internal/testutil/tempdir.go @@ -22,6 +22,7 @@ import ( "strings" "testing" + "sigs.k8s.io/krew/internal/gitutil" "sigs.k8s.io/yaml" ) @@ -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) + } +}