github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/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/docker/api/types"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
    14  	return &cobra.Command{
    15  		Use:     "rm NETWORK [NETWORK...]",
    16  		Aliases: []string{"remove"},
    17  		Short:   "Remove one or more networks",
    18  		Args:    cli.RequiresMinArgs(1),
    19  		RunE: func(cmd *cobra.Command, args []string) error {
    20  			return runRemove(dockerCli, args)
    21  		},
    22  	}
    23  }
    24  
    25  const ingressWarning = "WARNING! Before removing the routing-mesh network, " +
    26  	"make sure all the nodes in your swarm run the same docker engine version. " +
    27  	"Otherwise, removal may not be effective and functionality of newly create " +
    28  	"ingress networks will be impaired.\nAre you sure you want to continue?"
    29  
    30  func runRemove(dockerCli command.Cli, networks []string) error {
    31  	client := dockerCli.Client()
    32  	ctx := context.Background()
    33  	status := 0
    34  
    35  	for _, name := range networks {
    36  		if nw, _, err := client.NetworkInspectWithRaw(ctx, name, types.NetworkInspectOptions{}); err == nil &&
    37  			nw.Ingress &&
    38  			!command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), ingressWarning) {
    39  			continue
    40  		}
    41  		if err := client.NetworkRemove(ctx, name); err != nil {
    42  			fmt.Fprintf(dockerCli.Err(), "%s\n", err)
    43  			status = 1
    44  			continue
    45  		}
    46  		fmt.Fprintf(dockerCli.Out(), "%s\n", name)
    47  	}
    48  
    49  	if status != 0 {
    50  		return cli.StatusError{StatusCode: status}
    51  	}
    52  	return nil
    53  }