github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/network/remove.go (about) 1 package network 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/docker/cli/cli" 8 "github.com/docker/cli/cli/command" 9 "github.com/docker/cli/cli/command/completion" 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/errdefs" 12 "github.com/spf13/cobra" 13 ) 14 15 type removeOptions struct { 16 force bool 17 } 18 19 func newRemoveCommand(dockerCli command.Cli) *cobra.Command { 20 var opts removeOptions 21 22 cmd := &cobra.Command{ 23 Use: "rm NETWORK [NETWORK...]", 24 Aliases: []string{"remove"}, 25 Short: "Remove one or more networks", 26 Args: cli.RequiresMinArgs(1), 27 RunE: func(cmd *cobra.Command, args []string) error { 28 return runRemove(cmd.Context(), dockerCli, args, &opts) 29 }, 30 ValidArgsFunction: completion.NetworkNames(dockerCli), 31 } 32 33 flags := cmd.Flags() 34 flags.BoolVarP(&opts.force, "force", "f", false, "Do not error if the network does not exist") 35 return cmd 36 } 37 38 const ingressWarning = "WARNING! Before removing the routing-mesh network, " + 39 "make sure all the nodes in your swarm run the same docker engine version. " + 40 "Otherwise, removal may not be effective and functionality of newly create " + 41 "ingress networks will be impaired.\nAre you sure you want to continue?" 42 43 func runRemove(ctx context.Context, dockerCli command.Cli, networks []string, opts *removeOptions) error { 44 client := dockerCli.Client() 45 46 status := 0 47 48 for _, name := range networks { 49 if nw, _, err := client.NetworkInspectWithRaw(ctx, name, types.NetworkInspectOptions{}); err == nil && 50 nw.Ingress && 51 !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), ingressWarning) { 52 continue 53 } 54 if err := client.NetworkRemove(ctx, name); err != nil { 55 if opts.force && errdefs.IsNotFound(err) { 56 continue 57 } 58 fmt.Fprintf(dockerCli.Err(), "%s\n", err) 59 status = 1 60 continue 61 } 62 fmt.Fprintf(dockerCli.Out(), "%s\n", name) 63 } 64 65 if status != 0 { 66 return cli.StatusError{StatusCode: status} 67 } 68 return nil 69 }