github.1git.de/docker/cli@v26.1.3+incompatible/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  		nw, _, err := client.NetworkInspectWithRaw(ctx, name, types.NetworkInspectOptions{})
    50  		if err == nil && nw.Ingress {
    51  			r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), ingressWarning)
    52  			if err != nil {
    53  				return err
    54  			}
    55  			if !r {
    56  				continue
    57  			}
    58  		}
    59  		if err := client.NetworkRemove(ctx, name); err != nil {
    60  			if opts.force && errdefs.IsNotFound(err) {
    61  				continue
    62  			}
    63  			fmt.Fprintf(dockerCli.Err(), "%s\n", err)
    64  			status = 1
    65  			continue
    66  		}
    67  		fmt.Fprintf(dockerCli.Out(), "%s\n", name)
    68  	}
    69  
    70  	if status != 0 {
    71  		return cli.StatusError{StatusCode: status}
    72  	}
    73  	return nil
    74  }