github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/system/prune.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/cli"
     7  	"github.com/docker/docker/cli/command"
     8  	"github.com/docker/docker/cli/command/prune"
     9  	"github.com/docker/docker/opts"
    10  	units "github.com/docker/go-units"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type pruneOptions struct {
    15  	force  bool
    16  	all    bool
    17  	filter opts.FilterOpt
    18  }
    19  
    20  // NewPruneCommand creates a new cobra.Command for `docker prune`
    21  func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
    22  	opts := pruneOptions{filter: opts.NewFilterOpt()}
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "prune [OPTIONS]",
    26  		Short: "Remove unused data",
    27  		Args:  cli.NoArgs,
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			return runPrune(dockerCli, opts)
    30  		},
    31  		Tags: map[string]string{"version": "1.25"},
    32  	}
    33  
    34  	flags := cmd.Flags()
    35  	flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
    36  	flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images not just dangling ones")
    37  	flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
    38  
    39  	return cmd
    40  }
    41  
    42  const (
    43  	warning = `WARNING! This will remove:
    44  	- all stopped containers
    45  	- all volumes not used by at least one container
    46  	- all networks not used by at least one container
    47  	%s
    48  Are you sure you want to continue?`
    49  
    50  	danglingImageDesc = "- all dangling images"
    51  	allImageDesc      = `- all images without at least one container associated to them`
    52  )
    53  
    54  func runPrune(dockerCli *command.DockerCli, options pruneOptions) error {
    55  	var message string
    56  
    57  	if options.all {
    58  		message = fmt.Sprintf(warning, allImageDesc)
    59  	} else {
    60  		message = fmt.Sprintf(warning, danglingImageDesc)
    61  	}
    62  
    63  	if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
    64  		return nil
    65  	}
    66  
    67  	var spaceReclaimed uint64
    68  
    69  	for _, pruneFn := range []func(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error){
    70  		prune.RunContainerPrune,
    71  		prune.RunVolumePrune,
    72  		prune.RunNetworkPrune,
    73  	} {
    74  		spc, output, err := pruneFn(dockerCli, options.filter)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		spaceReclaimed += spc
    79  		if output != "" {
    80  			fmt.Fprintln(dockerCli.Out(), output)
    81  		}
    82  	}
    83  
    84  	spc, output, err := prune.RunImagePrune(dockerCli, options.all, options.filter)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	if spc > 0 {
    89  		spaceReclaimed += spc
    90  		fmt.Fprintln(dockerCli.Out(), output)
    91  	}
    92  
    93  	fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
    94  
    95  	return nil
    96  }