github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/cli/command/plugin/remove.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/cli"
     8  	"github.com/docker/docker/cli/command"
     9  	"github.com/docker/docker/reference"
    10  	"github.com/spf13/cobra"
    11  	"golang.org/x/net/context"
    12  )
    13  
    14  type rmOptions struct {
    15  	force bool
    16  
    17  	plugins []string
    18  }
    19  
    20  func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
    21  	var opts rmOptions
    22  
    23  	cmd := &cobra.Command{
    24  		Use:     "rm [OPTIONS] PLUGIN [PLUGIN...]",
    25  		Short:   "Remove one or more plugins",
    26  		Aliases: []string{"remove"},
    27  		Args:    cli.RequiresMinArgs(1),
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			opts.plugins = args
    30  			return runRemove(dockerCli, &opts)
    31  		},
    32  	}
    33  
    34  	flags := cmd.Flags()
    35  	flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of an active plugin")
    36  	return cmd
    37  }
    38  
    39  func runRemove(dockerCli *command.DockerCli, opts *rmOptions) error {
    40  	ctx := context.Background()
    41  
    42  	var errs cli.Errors
    43  	for _, name := range opts.plugins {
    44  		named, err := reference.ParseNamed(name) // FIXME: validate
    45  		if err != nil {
    46  			errs = append(errs, err)
    47  			continue
    48  		}
    49  		if reference.IsNameOnly(named) {
    50  			named = reference.WithDefaultTag(named)
    51  		}
    52  		ref, ok := named.(reference.NamedTagged)
    53  		if !ok {
    54  			errs = append(errs, fmt.Errorf("invalid name: %s", named.String()))
    55  			continue
    56  		}
    57  		// TODO: pass names to api instead of making multiple api calls
    58  		if err := dockerCli.Client().PluginRemove(ctx, ref.String(), types.PluginRemoveOptions{Force: opts.force}); err != nil {
    59  			errs = append(errs, err)
    60  			continue
    61  		}
    62  		fmt.Fprintln(dockerCli.Out(), name)
    63  	}
    64  	// Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value.
    65  	if errs != nil {
    66  		return errs
    67  	}
    68  	return nil
    69  }