Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / volume / prune.go
1 package volume
2
3 import (
4         "fmt"
5
6         "github.com/docker/cli/cli"
7         "github.com/docker/cli/cli/command"
8         "github.com/docker/cli/opts"
9         units "github.com/docker/go-units"
10         "github.com/spf13/cobra"
11         "golang.org/x/net/context"
12 )
13
14 type pruneOptions struct {
15         force  bool
16         filter opts.FilterOpt
17 }
18
19 // NewPruneCommand returns a new cobra prune command for volumes
20 func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
21         options := pruneOptions{filter: opts.NewFilterOpt()}
22
23         cmd := &cobra.Command{
24                 Use:   "prune [OPTIONS]",
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, options)
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                 Tags: map[string]string{"version": "1.25"},
39         }
40
41         flags := cmd.Flags()
42         flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
43         flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'label=<label>')")
44
45         return cmd
46 }
47
48 const warning = `WARNING! This will remove all volumes not used by at least one container.
49 Are you sure you want to continue?`
50
51 func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
52         pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
53
54         if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
55                 return
56         }
57
58         report, err := dockerCli.Client().VolumesPrune(context.Background(), pruneFilters)
59         if err != nil {
60                 return
61         }
62
63         if len(report.VolumesDeleted) > 0 {
64                 output = "Deleted Volumes:\n"
65                 for _, id := range report.VolumesDeleted {
66                         output += id + "\n"
67                 }
68                 spaceReclaimed = report.SpaceReclaimed
69         }
70
71         return
72 }
73
74 // RunPrune calls the Volume Prune API
75 // This returns the amount of space reclaimed and a detailed output string
76 func RunPrune(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) {
77         return runPrune(dockerCli, pruneOptions{force: true, filter: filter})
78 }