From 321f52d841154002332c10e8de444cc260938258 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Tue, 28 Apr 2020 00:08:05 -0700 Subject: [PATCH] multi-index: Add default index if none exists (#595) * multi-index: Add default index if none exists When user runs krew {install,update,upgrade} for the first time, if there are no indexes, default index (with krew-index repo) is now automatically added. Right now this code path only runs behind multi-index feature gate. Signed-off-by: Ahmet Alp Balkan * godoc fix Signed-off-by: Ahmet Alp Balkan --- cmd/krew/cmd/install.go | 2 +- cmd/krew/cmd/system.go | 2 +- cmd/krew/cmd/update.go | 34 +++++++++++++++++++-- cmd/krew/cmd/upgrade.go | 2 +- integration_test/index_test.go | 55 ++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 5 deletions(-) diff --git a/cmd/krew/cmd/install.go b/cmd/krew/cmd/install.go index cd510706..94f122f9 100644 --- a/cmd/krew/cmd/install.go +++ b/cmd/krew/cmd/install.go @@ -189,7 +189,7 @@ Remarks: klog.V(4).Infof("--no-update-index specified, skipping updating local copy of plugin index") return nil } - return ensureIndexesUpdated(cmd, args) + return ensureIndexes(cmd, args) }, } diff --git a/cmd/krew/cmd/system.go b/cmd/krew/cmd/system.go index 1ee44a54..dc6b5c2a 100644 --- a/cmd/krew/cmd/system.go +++ b/cmd/krew/cmd/system.go @@ -51,7 +51,7 @@ This command will be removed without further notice from future versions of krew RunE: func(cmd *cobra.Command, args []string) error { return receiptsmigration.Migrate(paths) }, - PreRunE: ensureIndexesUpdated, + PreRunE: func(_ *cobra.Command, _ []string) error { return ensureIndexesUpdated() }, } var indexUpgradeCmd = &cobra.Command{ diff --git a/cmd/krew/cmd/update.go b/cmd/krew/cmd/update.go index 1e053dd1..50fbbd60 100644 --- a/cmd/krew/cmd/update.go +++ b/cmd/krew/cmd/update.go @@ -44,7 +44,7 @@ plugin index from the internet. Remarks: You don't need to run this command: Running "krew update" or "krew upgrade" will silently run this command.`, - RunE: ensureIndexesUpdated, + RunE: ensureIndexes, } func showFormattedPluginsInfo(out io.Writer, header string, plugins []string) { @@ -120,7 +120,37 @@ func loadPlugins(indexes []indexoperations.Index) []pluginEntry { return out } -func ensureIndexesUpdated(_ *cobra.Command, _ []string) error { +func ensureIndexes(_ *cobra.Command, _ []string) error { + if os.Getenv(constants.EnableMultiIndexSwitch) != "" { + klog.V(3).Infof("Will check if there are any indexes added.") + if err := ensureDefaultIndexIfNoneExist(); err != nil { + return err + } + } + return ensureIndexesUpdated() +} + +// ensureDefaultIndexIfNoneExist adds the default index automatically +// (and informs the user about it) if no plugin index exists for krew. +func ensureDefaultIndexIfNoneExist() error { + idx, err := indexoperations.ListIndexes(paths) + if err != nil { + return errors.Wrap(err, "failed to retrieve plugin indexes") + } + if len(idx) > 0 { + klog.V(3).Infof("Found %d indexes, skipping adding default index.", len(idx)) + return nil + } + + klog.V(3).Infof("No index found, add default index.") + fmt.Fprintf(os.Stderr, "Adding \"default\" plugin index from %s.\n", constants.DefaultIndexURI) + return errors.Wrap(indexoperations.AddIndex(paths, constants.DefaultIndexName, constants.DefaultIndexURI), + "failed to add default plugin index in absence of no indexes") +} + +// ensureIndexesUpdated iterates over all indexes and updates them +// and prints new plugins and upgrades available for installed plugins. +func ensureIndexesUpdated() error { indexes, err := indexoperations.ListIndexes(paths) if err != nil { return errors.Wrap(err, "failed to list indexes") diff --git a/cmd/krew/cmd/upgrade.go b/cmd/krew/cmd/upgrade.go index 189d7f7a..a4f48c6e 100644 --- a/cmd/krew/cmd/upgrade.go +++ b/cmd/krew/cmd/upgrade.go @@ -106,7 +106,7 @@ kubectl krew upgrade foo bar"`, klog.V(4).Infof("--no-update-index specified, skipping updating local copy of plugin index") return nil } - return ensureIndexesUpdated(cmd, args) + return ensureIndexes(cmd, args) }, } diff --git a/integration_test/index_test.go b/integration_test/index_test.go index 9907febc..fa5b4911 100644 --- a/integration_test/index_test.go +++ b/integration_test/index_test.go @@ -16,6 +16,7 @@ package integrationtest import ( "os" + "regexp" "strings" "testing" @@ -171,3 +172,57 @@ func TestKrewIndexRemoveForce_nonExisting(t *testing.T) { // --force returns success for non-existing indexes test.Krew("index", "remove", "--force", "non-existing").RunOrFail() } + +func TestKrewDefaultIndex_notAutomaticallyAdded(t *testing.T) { + skipShort(t) + test, cleanup := NewTest(t) + test = test.WithEnv(constants.EnableMultiIndexSwitch, 1) + defer cleanup() + + test.Krew("help").RunOrFail() + out, err := test.Krew("search").Run() + if err == nil { + t.Fatalf("search must've failed without any indexes. output=%s", string(out)) + } + out = test.Krew("index", "list").RunOrFailOutput() + if len(lines(out)) > 1 { + t.Fatalf("expected no indexes; got output=%q", string(out)) + } +} + +func TestKrewDefaultIndex_AutoAddedOnInstall(t *testing.T) { + skipShort(t) + test, cleanup := NewTest(t) + test = test.WithEnv(constants.EnableMultiIndexSwitch, 1) + defer cleanup() + + test.Krew("install", validPlugin).RunOrFail() + ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput())) +} + +func TestKrewDefaultIndex_AutoAddedOnUpdate(t *testing.T) { + skipShort(t) + test, cleanup := NewTest(t) + test = test.WithEnv(constants.EnableMultiIndexSwitch, 1) + defer cleanup() + + test.Krew("update").RunOrFail() + ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput())) +} + +func TestKrewDefaultIndex_AutoAddedOnUpgrade(t *testing.T) { + skipShort(t) + test, cleanup := NewTest(t) + test = test.WithEnv(constants.EnableMultiIndexSwitch, 1) + defer cleanup() + + test.Krew("upgrade").RunOrFail() + ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput())) +} + +func ensureIndexListHasDefaultIndex(t *testing.T, output string) { + t.Helper() + if !regexp.MustCompile(`(?m)^default\b`).MatchString(output) { + t.Fatalf("index list did not return default index:\n%s", output) + } +}