github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/volume_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/volume"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newVolumeRmCommand() *cobra.Command {
    27  	volumeRmCommand := &cobra.Command{
    28  		Use:               "rm [flags] VOLUME [VOLUME...]",
    29  		Aliases:           []string{"remove"},
    30  		Short:             "Remove one or more volumes",
    31  		Long:              "NOTE: You cannot remove a volume that is in use by a container.",
    32  		Args:              cobra.MinimumNArgs(1),
    33  		RunE:              volumeRmAction,
    34  		ValidArgsFunction: volumeRmShellComplete,
    35  		SilenceUsage:      true,
    36  		SilenceErrors:     true,
    37  	}
    38  	volumeRmCommand.Flags().BoolP("force", "f", false, "(unimplemented yet)")
    39  	return volumeRmCommand
    40  }
    41  
    42  func processVolumeRmOptions(cmd *cobra.Command) (types.VolumeRemoveOptions, error) {
    43  	globalOptions, err := processRootCmdFlags(cmd)
    44  	if err != nil {
    45  		return types.VolumeRemoveOptions{}, err
    46  	}
    47  	force, err := cmd.Flags().GetBool("force")
    48  	if err != nil {
    49  		return types.VolumeRemoveOptions{}, err
    50  	}
    51  	return types.VolumeRemoveOptions{
    52  		GOptions: globalOptions,
    53  		Force:    force,
    54  		Stdout:   cmd.OutOrStdout(),
    55  	}, nil
    56  }
    57  
    58  func volumeRmAction(cmd *cobra.Command, args []string) error {
    59  	options, err := processVolumeRmOptions(cmd)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer cancel()
    69  
    70  	return volume.Remove(ctx, client, args, options)
    71  }
    72  
    73  func volumeRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    74  	// show volume names
    75  	return shellCompleteVolumeNames(cmd)
    76  }