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
17
17
import (
18
18
"flag"
19
19
"fmt"
20
+ "io/ioutil"
20
21
"math/rand"
21
22
"os"
22
23
"strings"
@@ -209,11 +210,24 @@ func cleanupStaleKrewInstallations() error {
209
210
}
210
211
211
212
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 {
215
218
return errors .New (`krew local plugin index is not initialized (run "kubectl krew update")` )
216
219
}
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
+ }
217
231
return nil
218
232
}
219
233
Original file line number Diff line number Diff line change @@ -213,6 +213,17 @@ func TestKrewDefaultIndex_AutoAddedOnUpgrade(t *testing.T) {
213
213
ensureIndexListHasDefaultIndex (t , string (test .Krew ("index" , "list" ).RunOrFailOutput ()))
214
214
}
215
215
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
+
216
227
func ensureIndexListHasDefaultIndex (t * testing.T , output string ) {
217
228
t .Helper ()
218
229
if ! regexp .MustCompile (`(?m)^default\b` ).MatchString (output ) {
You can’t perform that action at this time.
0 commit comments