From 7df322123290ee600812312421b98cfb97031e9f Mon Sep 17 00:00:00 2001
From: baude <bbaude@redhat.com>
Date: Mon, 13 Nov 2017 16:16:47 -0600
Subject: Remove all images

Add -a/--all to rmi so a user can remove
all images quickly.

Signed-off-by: baude <bbaude@redhat.com>

Closes: #41
Approved by: mheon
---
 cmd/kpod/rmi.go | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

(limited to 'cmd')

diff --git a/cmd/kpod/rmi.go b/cmd/kpod/rmi.go
index 3713db454..b69b3b514 100644
--- a/cmd/kpod/rmi.go
+++ b/cmd/kpod/rmi.go
@@ -4,12 +4,17 @@ import (
 	"fmt"
 
 	"github.com/pkg/errors"
+	"github.com/projectatomic/libpod/libpod"
 	"github.com/urfave/cli"
 )
 
 var (
 	rmiDescription = "removes one or more locally stored images."
 	rmiFlags       = []cli.Flag{
+		cli.BoolFlag{
+			Name:  "all, a",
+			Usage: "remove all images",
+		},
 		cli.BoolFlag{
 			Name:  "force, f",
 			Usage: "force removal of the image",
@@ -29,7 +34,7 @@ func rmiCmd(c *cli.Context) error {
 	if err := validateFlags(c, rmiFlags); err != nil {
 		return err
 	}
-
+	removeAll := c.Bool("all")
 	runtime, err := getRuntime(c)
 	if err != nil {
 		return errors.Wrapf(err, "could not get runtime")
@@ -37,11 +42,24 @@ func rmiCmd(c *cli.Context) error {
 	defer runtime.Shutdown(false)
 
 	args := c.Args()
-	if len(args) == 0 {
+	if len(args) == 0 && !removeAll {
 		return errors.Errorf("image name or ID must be specified")
 	}
+	if len(args) > 0 && removeAll {
+		return errors.Errorf("when using the --all switch, you may not pass any images names or IDs")
+	}
+	imagesToDelete := args[:]
+	if removeAll {
+		localImages, err := runtime.GetImages(&libpod.ImageFilterParams{})
+		if err != nil {
+			return errors.Wrapf(err, "unable to query local images")
+		}
+		for _, image := range localImages {
+			imagesToDelete = append(imagesToDelete, image.ID)
+		}
+	}
 
-	for _, arg := range args {
+	for _, arg := range imagesToDelete {
 		image, err := runtime.GetImage(arg)
 		if err != nil {
 			return errors.Wrapf(err, "could not get image %q", arg)
-- 
cgit v1.2.3-54-g00ecf