Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check all existing index #751

Merged
merged 3 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions cmd/krew/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
Expand Down Expand Up @@ -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.Errorf("invalid index %q, non git directory found in index folder", e.Name())
}
}
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions integration_test/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down