github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/volume/prune.go (about) 1 package volume 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 } 18 19 // NewPruneCommand returns a new cobra prune command for volumes 20 func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command { 21 var opts pruneOptions 22 23 cmd := &cobra.Command{ 24 Use: "prune", 25 Short: "Remove all unused volumes", 26 Args: cli.NoArgs, 27 RunE: func(cmd *cobra.Command, args []string) error { 28 spaceReclaimed, output, err := runPrune(dockerCli, opts) 29 if err != nil { 30 return err 31 } 32 if output != "" { 33 fmt.Fprintln(dockerCli.Out(), output) 34 } 35 fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed))) 36 return nil 37 }, 38 } 39 40 flags := cmd.Flags() 41 flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") 42 43 return cmd 44 } 45 46 const warning = `WARNING! This will remove all volumes not used by at least one container. 47 Are you sure you want to continue?` 48 49 func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) { 50 if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { 51 return 52 } 53 54 report, err := dockerCli.Client().VolumesPrune(context.Background(), types.VolumesPruneConfig{}) 55 if err != nil { 56 return 57 } 58 59 if len(report.VolumesDeleted) > 0 { 60 output = "Deleted Volumes:\n" 61 for _, id := range report.VolumesDeleted { 62 output += id + "\n" 63 } 64 spaceReclaimed = report.SpaceReclaimed 65 } 66 67 return 68 } 69 70 // RunPrune call the Volume Prune API 71 // This returns the amount of space reclaimed and a detailed output string 72 func RunPrune(dockerCli *command.DockerCli) (uint64, string, error) { 73 return runPrune(dockerCli, pruneOptions{force: true}) 74 }