@@ -285,39 +285,70 @@ var imageStatusCommand = cli.Command{
285
285
}
286
286
287
287
var removeImageCommand = cli.Command {
288
- Name : "rmi" ,
289
- Usage : "Remove one or more images" ,
290
- ArgsUsage : "IMAGE-ID [IMAGE-ID...]" ,
291
- Action : func (context * cli.Context ) error {
292
- if context .NArg () == 0 {
293
- return cli .ShowSubcommandHelp (context )
294
- }
295
- imageClient , conn , err := getImageClient (context )
288
+ Name : "rmi" ,
289
+ Usage : "Remove one or more images" ,
290
+ ArgsUsage : "IMAGE-ID [IMAGE-ID...]" ,
291
+ SkipArgReorder : true ,
292
+ UseShortOptionHandling : true ,
293
+ Flags : []cli.Flag {
294
+ cli.BoolFlag {
295
+ Name : "all, a" ,
296
+ Usage : "Remove all images" ,
297
+ },
298
+ },
299
+ Action : func (ctx * cli.Context ) error {
300
+ imageClient , conn , err := getImageClient (ctx )
296
301
if err != nil {
297
302
return err
298
303
}
299
- defer closeConnection (context , conn )
304
+ defer closeConnection (ctx , conn )
300
305
301
- for i := 0 ; i < context .NArg (); i ++ {
302
- id := context .Args ().Get (i )
306
+ ids := ctx .Args ()
307
+ if ctx .Bool ("all" ) {
308
+ r , err := imageClient .ListImages (context .Background (),
309
+ & pb.ListImagesRequest {})
310
+ if err != nil {
311
+ return err
312
+ }
313
+ ids = nil
314
+ for _ , img := range r .GetImages () {
315
+ ids = append (ids , img .GetId ())
316
+ }
317
+ }
318
+
319
+ if len (ids ) == 0 {
320
+ return cli .ShowSubcommandHelp (ctx )
321
+ }
303
322
304
- var verbose = false
305
- status , err := ImageStatus (imageClient , id , verbose )
323
+ errored := false
324
+ for _ , id := range ids {
325
+ status , err := ImageStatus (imageClient , id , false )
306
326
if err != nil {
307
- return fmt .Errorf ("image status request for %q failed: %v" , id , err )
327
+ logrus .Errorf ("image status request for %q failed: %v" , id , err )
328
+ errored = true
329
+ continue
308
330
}
309
331
if status .Image == nil {
310
- return fmt .Errorf ("no such image %s" , id )
332
+ logrus .Errorf ("no such image %s" , id )
333
+ errored = true
334
+ continue
311
335
}
312
336
313
337
_ , err = RemoveImage (imageClient , id )
314
338
if err != nil {
315
- return fmt .Errorf ("error of removing image %q: %v" , id , err )
339
+ logrus .Errorf ("error of removing image %q: %v" , id , err )
340
+ errored = true
341
+ continue
316
342
}
317
343
for _ , repoTag := range status .Image .RepoTags {
318
344
fmt .Printf ("Deleted: %s\n " , repoTag )
319
345
}
320
346
}
347
+
348
+ if errored {
349
+ return fmt .Errorf ("unable to remove the image(s)" )
350
+ }
351
+
321
352
return nil
322
353
},
323
354
}
0 commit comments