From 7fbb7bb30cdfa4db0a1a22a00dc2ff191d67ca79 Mon Sep 17 00:00:00 2001 From: "ye.sijun" Date: Thu, 30 Dec 2021 11:27:10 +0800 Subject: [PATCH 1/3] check all existing index Signed-off-by: ye.sijun --- cmd/krew/cmd/root.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/krew/cmd/root.go b/cmd/krew/cmd/root.go index 6ec27464..500ce2c8 100644 --- a/cmd/krew/cmd/root.go +++ b/cmd/krew/cmd/root.go @@ -17,6 +17,7 @@ package cmd import ( "flag" "fmt" + "io/ioutil" "math/rand" "os" "strings" @@ -209,11 +210,24 @@ func cleanupStaleKrewInstallations() error { } func checkIndex(_ *cobra.Command, _ []string) error { - if ok, err := gitutil.IsGitCloned(paths.IndexPath(constants.DefaultIndexName)); err != nil { - return errors.Wrap(err, "failed to check local index git repository") - } else if !ok { + entries, err := ioutil.ReadDir(paths.IndexBase()) + if err != nil { + return errors.Wrap(err, "failed to list directory") + } + if len(entries) == 0 { return errors.New(`krew local plugin index is not initialized (run "kubectl krew update")`) } + for _, e := range entries { + if !e.IsDir() { + continue + } + indexPath := paths.IndexPath(e.Name()) + if ok, err := gitutil.IsGitCloned(indexPath); err != nil { + return errors.Wrap(err, "failed to check local index git repository") + } else if !ok { + return errors.New(`krew local plugin index is not initialized (run "kubectl krew update")`) + } + } return nil } From 35c698146224ae0978f29220683833208e957cf2 Mon Sep 17 00:00:00 2001 From: "ye.sijun" Date: Fri, 14 Jan 2022 09:30:29 +0800 Subject: [PATCH 2/3] update error message Signed-off-by: ye.sijun --- cmd/krew/cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/krew/cmd/root.go b/cmd/krew/cmd/root.go index 500ce2c8..0d1d07ea 100644 --- a/cmd/krew/cmd/root.go +++ b/cmd/krew/cmd/root.go @@ -225,7 +225,7 @@ func checkIndex(_ *cobra.Command, _ []string) error { if ok, err := gitutil.IsGitCloned(indexPath); err != nil { return errors.Wrap(err, "failed to check local index git repository") } else if !ok { - return errors.New(`krew local plugin index is not initialized (run "kubectl krew update")`) + return errors.Errorf("invalid index %q, non git directory found in index folder", e.Name()) } } return nil From 47fbbbc836f442f2343e0ab7d7a4c52fa0649181 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Thu, 13 Jan 2022 21:36:49 -0800 Subject: [PATCH 3/3] add test case covering usage without default index --- integration_test/index_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/integration_test/index_test.go b/integration_test/index_test.go index ebf5dfc5..57c0cbb7 100644 --- a/integration_test/index_test.go +++ b/integration_test/index_test.go @@ -213,6 +213,17 @@ func TestKrewDefaultIndex_AutoAddedOnUpgrade(t *testing.T) { ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput())) } +func TestKrewOnlyCustomIndex(t *testing.T) { + skipShort(t) + test := NewTest(t) + out, err := test.Krew("list").Run() + if err == nil { + t.Fatalf("list should've failed without default index output=%s", string(out)) + } + test.Krew("index", "add", "custom-index", constants.DefaultIndexURI).RunOrFail() + test.Krew("list").RunOrFail() +} + func ensureIndexListHasDefaultIndex(t *testing.T, output string) { t.Helper() if !regexp.MustCompile(`(?m)^default\b`).MatchString(output) {