Skip to content

Commit 418de71

Browse files
authored
Merge pull request #523 from openSUSE/image-all-remove
Add `--all, -a` flag to image removal (`rmi`)
2 parents a1e9cf7 + c961c1b commit 418de71

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

cmd/crictl/image.go

+47-16
Original file line numberDiff line numberDiff line change
@@ -285,39 +285,70 @@ var imageStatusCommand = cli.Command{
285285
}
286286

287287
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)
296301
if err != nil {
297302
return err
298303
}
299-
defer closeConnection(context, conn)
304+
defer closeConnection(ctx, conn)
300305

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+
}
303322

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)
306326
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
308330
}
309331
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
311335
}
312336

313337
_, err = RemoveImage(imageClient, id)
314338
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
316342
}
317343
for _, repoTag := range status.Image.RepoTags {
318344
fmt.Printf("Deleted: %s\n", repoTag)
319345
}
320346
}
347+
348+
if errored {
349+
return fmt.Errorf("unable to remove the image(s)")
350+
}
351+
321352
return nil
322353
},
323354
}

0 commit comments

Comments
 (0)