diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 46c2fda7e7..be038463bc 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -284,39 +284,70 @@ var imageStatusCommand = cli.Command{ } var removeImageCommand = cli.Command{ - Name: "rmi", - Usage: "Remove one or more images", - ArgsUsage: "IMAGE-ID [IMAGE-ID...]", - Action: func(context *cli.Context) error { - if context.NArg() == 0 { - return cli.ShowSubcommandHelp(context) - } - imageClient, conn, err := getImageClient(context) + Name: "rmi", + Usage: "Remove one or more images", + ArgsUsage: "IMAGE-ID [IMAGE-ID...]", + SkipArgReorder: true, + UseShortOptionHandling: true, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "all, a", + Usage: "Remove all images", + }, + }, + Action: func(ctx *cli.Context) error { + imageClient, conn, err := getImageClient(ctx) if err != nil { return err } - defer closeConnection(context, conn) + defer closeConnection(ctx, conn) - for i := 0; i < context.NArg(); i++ { - id := context.Args().Get(i) + ids := ctx.Args() + if ctx.Bool("all") { + r, err := imageClient.ListImages(context.Background(), + &pb.ListImagesRequest{}) + if err != nil { + return err + } + ids = nil + for _, img := range r.GetImages() { + ids = append(ids, img.GetId()) + } + } + + if len(ids) == 0 { + return cli.ShowSubcommandHelp(ctx) + } - var verbose = false - status, err := ImageStatus(imageClient, id, verbose) + errored := false + for _, id := range ids { + status, err := ImageStatus(imageClient, id, false) if err != nil { - return fmt.Errorf("image status request for %q failed: %v", id, err) + logrus.Errorf("image status request for %q failed: %v", id, err) + errored = true + continue } if status.Image == nil { - return fmt.Errorf("no such image %s", id) + logrus.Errorf("no such image %s", id) + errored = true + continue } _, err = RemoveImage(imageClient, id) if err != nil { - return fmt.Errorf("error of removing image %q: %v", id, err) + logrus.Errorf("error of removing image %q: %v", id, err) + errored = true + continue } for _, repoTag := range status.Image.RepoTags { fmt.Printf("Deleted: %s\n", repoTag) } } + + if errored { + return fmt.Errorf("unable to remove the image(s)") + } + return nil }, }