2016-10-18 00:36:52 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-10-18 00:36:52 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-05-15 08:45:19 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-10-18 00:36:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := 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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
output, err := runPrune(dockerCli, options)
|
2016-10-18 00:36:52 -04:00
|
|
|
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()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
|
|
|
|
flags.Var(&options.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?`
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runPrune(dockerCli command.Cli, options pruneOptions) (output string, err error) {
|
|
|
|
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
|
2016-10-18 00:36:52 -04:00
|
|
|
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
|
2017-05-03 17:58:52 -04:00
|
|
|
func RunPrune(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) {
|
2016-12-07 17:02:13 -05:00
|
|
|
output, err := runPrune(dockerCli, pruneOptions{force: true, filter: filter})
|
2016-10-18 00:36:52 -04:00
|
|
|
return 0, output, err
|
|
|
|
}
|