github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_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/container"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newRmCommand() *cobra.Command {
    27  	var rmCommand = &cobra.Command{
    28  		Use:               "rm [flags] CONTAINER [CONTAINER, ...]",
    29  		Args:              cobra.MinimumNArgs(1),
    30  		Short:             "Remove one or more containers",
    31  		RunE:              rmAction,
    32  		ValidArgsFunction: rmShellComplete,
    33  		SilenceUsage:      true,
    34  		SilenceErrors:     true,
    35  	}
    36  	rmCommand.Flags().BoolP("force", "f", false, "Force the removal of a running|paused|unknown container (uses SIGKILL)")
    37  	rmCommand.Flags().BoolP("volumes", "v", false, "Remove volumes associated with the container")
    38  	return rmCommand
    39  }
    40  
    41  func rmAction(cmd *cobra.Command, args []string) error {
    42  	globalOptions, err := processRootCmdFlags(cmd)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	force, err := cmd.Flags().GetBool("force")
    47  	if err != nil {
    48  		return err
    49  	}
    50  	removeAnonVolumes, err := cmd.Flags().GetBool("volumes")
    51  	if err != nil {
    52  		return err
    53  	}
    54  	options := types.ContainerRemoveOptions{
    55  		GOptions: globalOptions,
    56  		Force:    force,
    57  		Volumes:  removeAnonVolumes,
    58  		Stdout:   cmd.OutOrStdout(),
    59  	}
    60  
    61  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	defer cancel()
    66  
    67  	return container.Remove(ctx, client, args, options)
    68  }
    69  
    70  func rmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    71  	// show container names
    72  	return shellCompleteContainerNames(cmd, nil)
    73  }