Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / system / prune.go
1 package system
2
3 import (
4         "fmt"
5
6         "github.com/docker/cli/cli"
7         "github.com/docker/cli/cli/command"
8         "github.com/docker/cli/cli/command/prune"
9         "github.com/docker/cli/opts"
10         units "github.com/docker/go-units"
11         "github.com/spf13/cobra"
12 )
13
14 type pruneOptions struct {
15         force        bool
16         all          bool
17         pruneVolumes bool
18         filter       opts.FilterOpt
19 }
20
21 // NewPruneCommand creates a new cobra.Command for `docker prune`
22 func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
23         options := pruneOptions{filter: opts.NewFilterOpt()}
24
25         cmd := &cobra.Command{
26                 Use:   "prune [OPTIONS]",
27                 Short: "Remove unused data",
28                 Args:  cli.NoArgs,
29                 RunE: func(cmd *cobra.Command, args []string) error {
30                         return runPrune(dockerCli, options)
31                 },
32                 Tags: map[string]string{"version": "1.25"},
33         }
34
35         flags := cmd.Flags()
36         flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
37         flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused images not just dangling ones")
38         flags.BoolVar(&options.pruneVolumes, "volumes", false, "Prune volumes")
39         flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'label=<key>=<value>')")
40         // "filter" flag is available in 1.28 (docker 17.04) and up
41         flags.SetAnnotation("filter", "version", []string{"1.28"})
42
43         return cmd
44 }
45
46 const (
47         warning = `WARNING! This will remove:
48         - all stopped containers
49         - all volumes not used by at least one container
50         - all networks not used by at least one container
51         %s
52 Are you sure you want to continue?`
53
54         danglingImageDesc = "- all dangling images"
55         allImageDesc      = `- all images without at least one container associated to them`
56 )
57
58 func runPrune(dockerCli command.Cli, options pruneOptions) error {
59         var message string
60
61         if options.all {
62                 message = fmt.Sprintf(warning, allImageDesc)
63         } else {
64                 message = fmt.Sprintf(warning, danglingImageDesc)
65         }
66
67         if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
68                 return nil
69         }
70
71         var spaceReclaimed uint64
72         pruneFuncs := []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){
73                 prune.RunContainerPrune,
74                 prune.RunNetworkPrune,
75         }
76         if options.pruneVolumes {
77                 pruneFuncs = append(pruneFuncs, prune.RunVolumePrune)
78         }
79
80         for _, pruneFn := range pruneFuncs {
81                 spc, output, err := pruneFn(dockerCli, options.filter)
82                 if err != nil {
83                         return err
84                 }
85                 spaceReclaimed += spc
86                 if output != "" {
87                         fmt.Fprintln(dockerCli.Out(), output)
88                 }
89         }
90
91         spc, output, err := prune.RunImagePrune(dockerCli, options.all, options.filter)
92         if err != nil {
93                 return err
94         }
95         if spc > 0 {
96                 spaceReclaimed += spc
97                 fmt.Fprintln(dockerCli.Out(), output)
98         }
99
100         fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
101
102         return nil
103 }