2016-09-22 17:04:34 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-12-07 17:02:13 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2016-09-22 17:04:34 -04:00
|
|
|
units "github.com/docker/go-units"
|
|
|
|
"github.com/spf13/cobra"
|
2016-12-24 19:05:37 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-09-22 17:04:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type pruneOptions struct {
|
2016-12-07 17:02:13 -05:00
|
|
|
force bool
|
|
|
|
filter opts.FilterOpt
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPruneCommand returns a new cobra prune command for containers
|
|
|
|
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-12-07 17:02:13 -05:00
|
|
|
opts := pruneOptions{filter: opts.NewFilterOpt()}
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-10-10 11:07:32 -04:00
|
|
|
Use: "prune [OPTIONS]",
|
2016-09-22 17:04:34 -04:00
|
|
|
Short: "Remove all stopped containers",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
spaceReclaimed, output, err := runPrune(dockerCli, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if output != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), output)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
|
|
|
|
return nil
|
|
|
|
},
|
2016-11-02 20:43:32 -04:00
|
|
|
Tags: map[string]string{"version": "1.25"},
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
|
2016-12-07 17:02:13 -05:00
|
|
|
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
const warning = `WARNING! This will remove all stopped containers.
|
2016-11-03 13:33:17 -04:00
|
|
|
Are you sure you want to continue?`
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) {
|
2017-02-04 12:10:05 -05:00
|
|
|
pruneFilters := command.PruneFilters(dockerCli, opts.filter.Value())
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-09-22 17:04:34 -04:00
|
|
|
if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
report, err := dockerCli.Client().ContainersPrune(context.Background(), pruneFilters)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(report.ContainersDeleted) > 0 {
|
2016-10-18 02:20:12 -04:00
|
|
|
output = "Deleted Containers:\n"
|
2016-09-22 17:04:34 -04:00
|
|
|
for _, id := range report.ContainersDeleted {
|
|
|
|
output += id + "\n"
|
|
|
|
}
|
|
|
|
spaceReclaimed = report.SpaceReclaimed
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// RunPrune calls the Container Prune API
|
2016-09-22 17:04:34 -04:00
|
|
|
// This returns the amount of space reclaimed and a detailed output string
|
2016-12-07 17:02:13 -05:00
|
|
|
func RunPrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) {
|
|
|
|
return runPrune(dockerCli, pruneOptions{force: true, filter: filter})
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|