Skip to content

Commit

Permalink
check all existing index (#751)
Browse files Browse the repository at this point in the history
* check all existing index

Signed-off-by: ye.sijun <[email protected]>

* update error message

Signed-off-by: ye.sijun <[email protected]>

* add test case covering usage without default index

Co-authored-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
junnplus and ahmetb authored Jan 14, 2022
1 parent b015efb commit 0517b50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
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

0 comments on commit 0517b50

Please sign in to comment.