2016-10-18 00:36:52 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-12-07 17:02:13 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2016-10-18 00:36:52 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pruneOptions struct {
|
2016-12-07 17:02:13 -05:00
|
|
|
force bool
|
|
|
|
filter opts.FilterOpt
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPruneCommand returns a new cobra prune command for networks
|
|
|
|
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-12-07 17:02:13 -05:00
|
|
|
opts := pruneOptions{filter: opts.NewFilterOpt()}
|
2016-10-18 00:36:52 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "prune [OPTIONS]",
|
|
|
|
Short: "Remove all unused networks",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
output, err := runPrune(dockerCli, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if output != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2016-11-02 20:43:32 -04:00
|
|
|
Tags: map[string]string{"version": "1.25"},
|
2016-10-18 00:36:52 -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-10-18 00:36:52 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
const warning = `WARNING! This will remove all networks not used by at least one container.
|
|
|
|
Are you sure you want to continue?`
|
|
|
|
|
|
|
|
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (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-10-18 00:36:52 -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().NetworksPrune(context.Background(), pruneFilters)
|
2016-10-18 00:36:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(report.NetworksDeleted) > 0 {
|
|
|
|
output = "Deleted Networks:\n"
|
|
|
|
for _, id := range report.NetworksDeleted {
|
|
|
|
output += id + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunPrune calls the Network Prune API
|
|
|
|
// 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) {
|
|
|
|
output, err := runPrune(dockerCli, pruneOptions{force: true, filter: filter})
|
2016-10-18 00:36:52 -04:00
|
|
|
return 0, output, err
|
|
|
|
}
|