github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/containers_prune.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/containers/libpod/cmd/podman/cliconfig"
    10  	"github.com/containers/libpod/cmd/podman/shared"
    11  	"github.com/containers/libpod/libpod/define"
    12  	"github.com/containers/libpod/pkg/adapter"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var (
    18  	pruneContainersCommand     cliconfig.PruneContainersValues
    19  	pruneContainersDescription = `
    20  	podman container prune
    21  
    22  	Removes all exited containers
    23  `
    24  	_pruneContainersCommand = &cobra.Command{
    25  		Use:   "prune",
    26  		Args:  noSubArgs,
    27  		Short: "Remove all stopped containers",
    28  		Long:  pruneContainersDescription,
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			pruneContainersCommand.InputArgs = args
    31  			pruneContainersCommand.GlobalFlags = MainGlobalOpts
    32  			pruneContainersCommand.Remote = remoteclient
    33  			return pruneContainersCmd(&pruneContainersCommand)
    34  		},
    35  	}
    36  )
    37  
    38  func init() {
    39  	pruneContainersCommand.Command = _pruneContainersCommand
    40  	pruneContainersCommand.SetHelpTemplate(HelpTemplate())
    41  	pruneContainersCommand.SetUsageTemplate(UsageTemplate())
    42  	flags := pruneContainersCommand.Flags()
    43  	flags.BoolVarP(&pruneContainersCommand.Force, "force", "f", false, "Skip interactive prompt for container removal")
    44  	flags.StringArrayVar(&pruneContainersCommand.Filter, "filter", []string{}, "Provide filter values (e.g. 'until=<timestamp>')")
    45  }
    46  
    47  func pruneContainersCmd(c *cliconfig.PruneContainersValues) error {
    48  	if !c.Force {
    49  		reader := bufio.NewReader(os.Stdin)
    50  		fmt.Printf(`WARNING! This will remove all stopped containers.
    51  Are you sure you want to continue? [y/N] `)
    52  		answer, err := reader.ReadString('\n')
    53  		if err != nil {
    54  			return errors.Wrapf(err, "error reading input")
    55  		}
    56  		if strings.ToLower(answer)[0] != 'y' {
    57  			return nil
    58  		}
    59  	}
    60  
    61  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    62  	if err != nil {
    63  		return errors.Wrapf(err, "could not get runtime")
    64  	}
    65  	defer runtime.DeferredShutdown(false)
    66  
    67  	maxWorkers := shared.DefaultPoolSize("prune")
    68  	if c.GlobalIsSet("max-workers") {
    69  		maxWorkers = c.GlobalFlags.MaxWorks
    70  	}
    71  	ok, failures, err := runtime.Prune(getContext(), maxWorkers, c.Filter)
    72  	if err != nil {
    73  		if errors.Cause(err) == define.ErrNoSuchCtr {
    74  			if len(c.InputArgs) > 1 {
    75  				exitCode = define.ExecErrorCodeGeneric
    76  			} else {
    77  				exitCode = 1
    78  			}
    79  		}
    80  		return err
    81  	}
    82  	if len(failures) > 0 {
    83  		exitCode = define.ExecErrorCodeGeneric
    84  	}
    85  	return printCmdResults(ok, failures)
    86  }