github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/network/prune.go (about) 1 package network 2 3 import ( 4 "fmt" 5 6 "golang.org/x/net/context" 7 8 "github.com/docker/docker/cli" 9 "github.com/docker/docker/cli/command" 10 "github.com/docker/docker/opts" 11 "github.com/spf13/cobra" 12 ) 13 14 type pruneOptions struct { 15 force bool 16 filter opts.FilterOpt 17 } 18 19 // NewPruneCommand returns a new cobra prune command for networks 20 func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command { 21 opts := pruneOptions{filter: opts.NewFilterOpt()} 22 23 cmd := &cobra.Command{ 24 Use: "prune [OPTIONS]", 25 Short: "Remove all unused networks", 26 Args: cli.NoArgs, 27 RunE: func(cmd *cobra.Command, args []string) error { 28 output, err := runPrune(dockerCli, opts) 29 if err != nil { 30 return err 31 } 32 if output != "" { 33 fmt.Fprintln(dockerCli.Out(), output) 34 } 35 return nil 36 }, 37 Tags: map[string]string{"version": "1.25"}, 38 } 39 40 flags := cmd.Flags() 41 flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") 42 flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')") 43 44 return cmd 45 } 46 47 const warning = `WARNING! This will remove all networks not used by at least one container. 48 Are you sure you want to continue?` 49 50 func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (output string, err error) { 51 pruneFilters := opts.filter.Value() 52 53 if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { 54 return 55 } 56 57 report, err := dockerCli.Client().NetworksPrune(context.Background(), pruneFilters) 58 if err != nil { 59 return 60 } 61 62 if len(report.NetworksDeleted) > 0 { 63 output = "Deleted Networks:\n" 64 for _, id := range report.NetworksDeleted { 65 output += id + "\n" 66 } 67 } 68 69 return 70 } 71 72 // RunPrune calls the Network Prune API 73 // This returns the amount of space reclaimed and a detailed output string 74 func RunPrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) { 75 output, err := runPrune(dockerCli, pruneOptions{force: true, filter: filter}) 76 return 0, output, err 77 }