Skip to content

Commit

Permalink
multi-index: Add default index if none exists (#595)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* godoc fix

Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb authored Apr 28, 2020
1 parent 045262e commit 321f52d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/krew/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/krew/cmd/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
34 changes: 32 additions & 2 deletions cmd/krew/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/krew/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
55 changes: 55 additions & 0 deletions integration_test/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package integrationtest

import (
"os"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 321f52d

Please sign in to comment.