File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ package cmd
1717import (
1818 "flag"
1919 "fmt"
20+ "io/ioutil"
2021 "math/rand"
2122 "os"
2223 "strings"
@@ -209,11 +210,24 @@ func cleanupStaleKrewInstallations() error {
209210}
210211
211212func 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
Original file line number Diff line number Diff 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+
216227func ensureIndexListHasDefaultIndex (t * testing.T , output string ) {
217228 t .Helper ()
218229 if ! regexp .MustCompile (`(?m)^default\b` ).MatchString (output ) {
You can’t perform that action at this time.
0 commit comments