summaryrefslogtreecommitdiff
path: root/cmd/podmanV2
diff options
context:
space:
mode:
authorSujil02 <sushah@redhat.com>2020-04-08 04:49:32 -0400
committerSujil02 <sushah@redhat.com>2020-04-15 11:17:33 -0400
commitec4060aef6c77c049fc2c3b6438ebb5590f6ab69 (patch)
tree2ce43bc8b0a0c0b9c9bdf1d404c6339e06441a6b /cmd/podmanV2
parent37ed662f323ef4700ae14d441fb2264a59960baa (diff)
downloadpodman-ec4060aef6c77c049fc2c3b6438ebb5590f6ab69.tar.gz
podman-ec4060aef6c77c049fc2c3b6438ebb5590f6ab69.tar.bz2
podman-ec4060aef6c77c049fc2c3b6438ebb5590f6ab69.zip
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>
Diffstat (limited to 'cmd/podmanV2')
-rw-r--r--cmd/podmanV2/containers/prune.go86
1 files changed, 86 insertions, 0 deletions
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()
+}