github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/network_remove.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/containerd/nerdctl/pkg/api/types"
    21  	"github.com/containerd/nerdctl/pkg/clientutil"
    22  	"github.com/containerd/nerdctl/pkg/cmd/network"
    23  	"github.com/containerd/nerdctl/pkg/netutil"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func newNetworkRmCommand() *cobra.Command {
    29  	networkRmCommand := &cobra.Command{
    30  		Use:               "rm [flags] NETWORK [NETWORK, ...]",
    31  		Aliases:           []string{"remove"},
    32  		Short:             "Remove one or more networks",
    33  		Long:              "NOTE: network in use is deleted without caution",
    34  		Args:              cobra.MinimumNArgs(1),
    35  		RunE:              networkRmAction,
    36  		ValidArgsFunction: networkRmShellComplete,
    37  		SilenceUsage:      true,
    38  		SilenceErrors:     true,
    39  	}
    40  	return networkRmCommand
    41  }
    42  
    43  func networkRmAction(cmd *cobra.Command, args []string) error {
    44  	globalOptions, err := processRootCmdFlags(cmd)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	options := types.NetworkRemoveOptions{
    50  		GOptions: globalOptions,
    51  		Networks: args,
    52  		Stdout:   cmd.OutOrStdout(),
    53  	}
    54  
    55  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	defer cancel()
    60  
    61  	return network.Remove(ctx, client, options)
    62  }
    63  
    64  func networkRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    65  	// show network names, including "bridge"
    66  	exclude := []string{netutil.DefaultNetworkName, "host", "none"}
    67  	return shellCompleteNetworkNames(cmd, exclude)
    68  }