Skip to content

Commit 0517b50

Browse files
junnplusahmetb
andauthored
check all existing index (#751)
* 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]>
1 parent b015efb commit 0517b50

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

cmd/krew/cmd/root.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717
import (
1818
"flag"
1919
"fmt"
20+
"io/ioutil"
2021
"math/rand"
2122
"os"
2223
"strings"
@@ -209,11 +210,24 @@ func cleanupStaleKrewInstallations() error {
209210
}
210211

211212
func checkIndex(_ *cobra.Command, _ []string) error {
212-
if ok, err := gitutil.IsGitCloned(paths.IndexPath(constants.DefaultIndexName)); err != nil {
213-
return errors.Wrap(err, "failed to check local index git repository")
214-
} else if !ok {
213+
entries, err := ioutil.ReadDir(paths.IndexBase())
214+
if err != nil {
215+
return errors.Wrap(err, "failed to list directory")
216+
}
217+
if len(entries) == 0 {
215218
return errors.New(`krew local plugin index is not initialized (run "kubectl krew update")`)
216219
}
220+
for _, e := range entries {
221+
if !e.IsDir() {
222+
continue
223+
}
224+
indexPath := paths.IndexPath(e.Name())
225+
if ok, err := gitutil.IsGitCloned(indexPath); err != nil {
226+
return errors.Wrap(err, "failed to check local index git repository")
227+
} else if !ok {
228+
return errors.Errorf("invalid index %q, non git directory found in index folder", e.Name())
229+
}
230+
}
217231
return nil
218232
}
219233

integration_test/index_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@ func TestKrewDefaultIndex_AutoAddedOnUpgrade(t *testing.T) {
213213
ensureIndexListHasDefaultIndex(t, string(test.Krew("index", "list").RunOrFailOutput()))
214214
}
215215

216+
func TestKrewOnlyCustomIndex(t *testing.T) {
217+
skipShort(t)
218+
test := NewTest(t)
219+
out, err := test.Krew("list").Run()
220+
if err == nil {
221+
t.Fatalf("list should've failed without default index output=%s", string(out))
222+
}
223+
test.Krew("index", "add", "custom-index", constants.DefaultIndexURI).RunOrFail()
224+
test.Krew("list").RunOrFail()
225+
}
226+
216227
func ensureIndexListHasDefaultIndex(t *testing.T, output string) {
217228
t.Helper()
218229
if !regexp.MustCompile(`(?m)^default\b`).MatchString(output) {

0 commit comments

Comments
 (0)