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