From ec4060aef6c77c049fc2c3b6438ebb5590f6ab69 Mon Sep 17 00:00:00 2001
From: Sujil02 <sushah@redhat.com>
Date: Wed, 8 Apr 2020 04:49:32 -0400
Subject: Ability to prune container in api V2

Adds ability to prune containers for v2.
Adds client side prompt with force flag and filters options to prune.

Signed-off-by: Sujil02 <sushah@redhat.com>
---
 cmd/podman/containers_prune.go   |  4 +-
 cmd/podmanV2/containers/prune.go | 86 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100644 cmd/podmanV2/containers/prune.go

(limited to 'cmd')

diff --git a/cmd/podman/containers_prune.go b/cmd/podman/containers_prune.go
index cd9817e7e..3953a489d 100644
--- a/cmd/podman/containers_prune.go
+++ b/cmd/podman/containers_prune.go
@@ -19,12 +19,12 @@ var (
 	pruneContainersDescription = `
 	podman container prune
 
-	Removes all exited containers
+	Removes all stopped | exited containers
 `
 	_pruneContainersCommand = &cobra.Command{
 		Use:   "prune",
 		Args:  noSubArgs,
-		Short: "Remove all stopped containers",
+		Short: "Remove all stopped | exited containers",
 		Long:  pruneContainersDescription,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			pruneContainersCommand.InputArgs = args
diff --git a/cmd/podmanV2/containers/prune.go b/cmd/podmanV2/containers/prune.go
new file mode 100644
index 000000000..2d3af5d1d
--- /dev/null
+++ b/cmd/podmanV2/containers/prune.go
@@ -0,0 +1,86 @@
+package containers
+
+import (
+	"bufio"
+	"context"
+	"fmt"
+	"net/url"
+	"os"
+	"strings"
+
+	"github.com/containers/libpod/cmd/podmanV2/registry"
+	"github.com/containers/libpod/cmd/podmanV2/utils"
+	"github.com/containers/libpod/pkg/domain/entities"
+	"github.com/pkg/errors"
+	"github.com/spf13/cobra"
+)
+
+var (
+	pruneDescription = fmt.Sprintf(`podman container prune
+
+	Removes all stopped | exited containers`)
+	pruneCommand = &cobra.Command{
+		Use:     "prune [flags]",
+		Short:   "Remove all stopped | exited containers",
+		Long:    pruneDescription,
+		RunE:    prune,
+		Example: `podman container prune`,
+	}
+	force  bool
+	filter = []string{}
+)
+
+func init() {
+	registry.Commands = append(registry.Commands, registry.CliCommand{
+		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+		Command: pruneCommand,
+		Parent:  containerCmd,
+	})
+	flags := pruneCommand.Flags()
+	flags.BoolVarP(&force, "force", "f", false, "Do not prompt for confirmation.  The default is false")
+	flags.StringArrayVar(&filter, "filter", []string{}, "Provide filter values (e.g. 'label=<key>=<value>')")
+}
+
+func prune(cmd *cobra.Command, args []string) error {
+	var (
+		errs         utils.OutputErrors
+		pruneOptions = entities.ContainerPruneOptions{}
+	)
+	if len(args) > 0 {
+		return errors.Errorf("`%s` takes no arguments", cmd.CommandPath())
+	}
+	if !force {
+		reader := bufio.NewReader(os.Stdin)
+		fmt.Println("WARNING! This will remove all stopped containers.")
+		fmt.Print("Are you sure you want to continue? [y/N] ")
+		answer, err := reader.ReadString('\n')
+		if err != nil {
+			return errors.Wrapf(err, "error reading input")
+		}
+		if strings.ToLower(answer)[0] != 'y' {
+			return nil
+		}
+	}
+
+	// TODO Remove once filter refactor is finished and url.Values done.
+	for _, f := range filter {
+		t := strings.SplitN(f, "=", 2)
+		pruneOptions.Filters = make(url.Values)
+		if len(t) < 2 {
+			return errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
+		}
+		pruneOptions.Filters.Add(t[0], t[1])
+	}
+	responses, err := registry.ContainerEngine().ContainerPrune(context.Background(), pruneOptions)
+
+	if err != nil {
+		return err
+	}
+	for k := range responses.ID {
+		fmt.Println(k)
+	}
+	for _, v := range responses.Err {
+		errs = append(errs, v)
+	}
+	return errs.PrintErrors()
+}
-- 
cgit v1.2.3-54-g00ecf