github.com/olljanat/moby@v1.13.1/cli/command/image/prune.go (about)

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/types/filters"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/docker/docker/cli/command"
    11  	units "github.com/docker/go-units"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type pruneOptions struct {
    16  	force bool
    17  	all   bool
    18  }
    19  
    20  // NewPruneCommand returns a new cobra prune command for images
    21  func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
    22  	var opts pruneOptions
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "prune [OPTIONS]",
    26  		Short: "Remove unused images",
    27  		Args:  cli.NoArgs,
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			spaceReclaimed, output, err := runPrune(dockerCli, opts)
    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  		Tags: map[string]string{"version": "1.25"},
    40  	}
    41  
    42  	flags := cmd.Flags()
    43  	flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
    44  	flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images, not just dangling ones")
    45  
    46  	return cmd
    47  }
    48  
    49  const (
    50  	allImageWarning = `WARNING! This will remove all images without at least one container associated to them.
    51  Are you sure you want to continue?`
    52  	danglingWarning = `WARNING! This will remove all dangling images.
    53  Are you sure you want to continue?`
    54  )
    55  
    56  func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) {
    57  	pruneFilters := filters.NewArgs()
    58  	pruneFilters.Add("dangling", fmt.Sprintf("%v", !opts.all))
    59  
    60  	warning := danglingWarning
    61  	if opts.all {
    62  		warning = allImageWarning
    63  	}
    64  	if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
    65  		return
    66  	}
    67  
    68  	report, err := dockerCli.Client().ImagesPrune(context.Background(), pruneFilters)
    69  	if err != nil {
    70  		return
    71  	}
    72  
    73  	if len(report.ImagesDeleted) > 0 {
    74  		output = "Deleted Images:\n"
    75  		for _, st := range report.ImagesDeleted {
    76  			if st.Untagged != "" {
    77  				output += fmt.Sprintln("untagged:", st.Untagged)
    78  			} else {
    79  				output += fmt.Sprintln("deleted:", st.Deleted)
    80  			}
    81  		}
    82  		spaceReclaimed = report.SpaceReclaimed
    83  	}
    84  
    85  	return
    86  }
    87  
    88  // RunPrune calls the Image Prune API
    89  // This returns the amount of space reclaimed and a detailed output string
    90  func RunPrune(dockerCli *command.DockerCli, all bool) (uint64, string, error) {
    91  	return runPrune(dockerCli, pruneOptions{force: true, all: all})
    92  }