github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/container/prune.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/cli/cli/command/completion"
    10  	"github.com/docker/cli/opts"
    11  	units "github.com/docker/go-units"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type pruneOptions struct {
    16  	force  bool
    17  	filter opts.FilterOpt
    18  }
    19  
    20  // NewPruneCommand returns a new cobra prune command for containers
    21  func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
    22  	options := pruneOptions{filter: opts.NewFilterOpt()}
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "prune [OPTIONS]",
    26  		Short: "Remove all stopped containers",
    27  		Args:  cli.NoArgs,
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options)
    30  			if err != nil {
    31  				return err
    32  			}
    33  			if output != "" {
    34  				fmt.Fprintln(dockerCli.Out(), output)
    35  			}
    36  			fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
    37  			return nil
    38  		},
    39  		Annotations:       map[string]string{"version": "1.25"},
    40  		ValidArgsFunction: completion.NoComplete,
    41  	}
    42  
    43  	flags := cmd.Flags()
    44  	flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
    45  	flags.Var(&options.filter, "filter", `Provide filter values (e.g. "until=<timestamp>")`)
    46  
    47  	return cmd
    48  }
    49  
    50  const warning = `WARNING! This will remove all stopped containers.
    51  Are you sure you want to continue?`
    52  
    53  func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
    54  	pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
    55  
    56  	if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
    57  		return 0, "", nil
    58  	}
    59  
    60  	report, err := dockerCli.Client().ContainersPrune(ctx, pruneFilters)
    61  	if err != nil {
    62  		return 0, "", err
    63  	}
    64  
    65  	if len(report.ContainersDeleted) > 0 {
    66  		output = "Deleted Containers:\n"
    67  		for _, id := range report.ContainersDeleted {
    68  			output += id + "\n"
    69  		}
    70  		spaceReclaimed = report.SpaceReclaimed
    71  	}
    72  
    73  	return spaceReclaimed, output, nil
    74  }
    75  
    76  // RunPrune calls the Container Prune API
    77  // This returns the amount of space reclaimed and a detailed output string
    78  func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) {
    79  	return runPrune(ctx, dockerCli, pruneOptions{force: true, filter: filter})
    80  }