github.com/kunnos/engine@v1.13.1/cli/command/volume/remove.go (about)

     1  package volume
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/cli"
     9  	"github.com/docker/docker/cli/command"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type removeOptions struct {
    14  	force bool
    15  
    16  	volumes []string
    17  }
    18  
    19  func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
    20  	var opts removeOptions
    21  
    22  	cmd := &cobra.Command{
    23  		Use:     "rm [OPTIONS] VOLUME [VOLUME...]",
    24  		Aliases: []string{"remove"},
    25  		Short:   "Remove one or more volumes",
    26  		Long:    removeDescription,
    27  		Example: removeExample,
    28  		Args:    cli.RequiresMinArgs(1),
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			opts.volumes = args
    31  			return runRemove(dockerCli, &opts)
    32  		},
    33  	}
    34  
    35  	flags := cmd.Flags()
    36  	flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of one or more volumes")
    37  	flags.SetAnnotation("force", "version", []string{"1.25"})
    38  	return cmd
    39  }
    40  
    41  func runRemove(dockerCli *command.DockerCli, opts *removeOptions) error {
    42  	client := dockerCli.Client()
    43  	ctx := context.Background()
    44  	status := 0
    45  
    46  	for _, name := range opts.volumes {
    47  		if err := client.VolumeRemove(ctx, name, opts.force); err != nil {
    48  			fmt.Fprintf(dockerCli.Err(), "%s\n", err)
    49  			status = 1
    50  			continue
    51  		}
    52  		fmt.Fprintf(dockerCli.Out(), "%s\n", name)
    53  	}
    54  
    55  	if status != 0 {
    56  		return cli.StatusError{StatusCode: status}
    57  	}
    58  	return nil
    59  }
    60  
    61  var removeDescription = `
    62  Remove one or more volumes. You cannot remove a volume that is in use by a container.
    63  `
    64  
    65  var removeExample = `
    66  $ docker volume rm hello
    67  hello
    68  `