|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "golang.org/x/net/context" |
| 7 | + |
| 8 | + "github.com/docker/docker/api/types" |
| 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", |
| 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 | + } |
| 40 | + |
| 41 | + flags := cmd.Flags() |
| 42 | + flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") |
| 43 | + flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images, not just dangling ones") |
| 44 | + |
| 45 | + return cmd |
| 46 | +} |
| 47 | + |
| 48 | +const ( |
| 49 | + allImageWarning = `WARNING! This will remove all images without at least one container associated to them. |
| 50 | +Are you sure you want to continue?` |
| 51 | + danglingWarning = `WARNING! This will remove all dangling images. |
| 52 | +Are you sure you want to continue?` |
| 53 | +) |
| 54 | + |
| 55 | +func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) { |
| 56 | + warning := danglingWarning |
| 57 | + if opts.all { |
| 58 | + warning = allImageWarning |
| 59 | + } |
| 60 | + if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + report, err := dockerCli.Client().ImagesPrune(context.Background(), types.ImagesPruneConfig{ |
| 65 | + DanglingOnly: !opts.all, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + if len(report.ImagesDeleted) > 0 { |
| 72 | + output = "Deleted Images:\n" |
| 73 | + for _, st := range report.ImagesDeleted { |
| 74 | + if st.Untagged != "" { |
| 75 | + output += fmt.Sprintln("untagged:", st.Untagged) |
| 76 | + } else { |
| 77 | + output += fmt.Sprintln("deleted:", st.Deleted) |
| 78 | + } |
| 79 | + } |
| 80 | + spaceReclaimed = report.SpaceReclaimed |
| 81 | + } |
| 82 | + |
| 83 | + return |
| 84 | +} |
| 85 | + |
| 86 | +// RunPrune call the Image Prune API |
| 87 | +// This returns the amount of space reclaimed and a detailed output string |
| 88 | +func RunPrune(dockerCli *command.DockerCli, all bool) (uint64, string, error) { |
| 89 | + return runPrune(dockerCli, pruneOptions{force: true, all: all}) |
| 90 | +} |
0 commit comments