github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/plugin/remove.go (about)

     1  // +build experimental
     2  
     3  package plugin
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/docker/docker/cli/command"
    11  	"github.com/docker/docker/reference"
    12  	"github.com/spf13/cobra"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  type rmOptions struct {
    17  	force bool
    18  
    19  	plugins []string
    20  }
    21  
    22  func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
    23  	var opts rmOptions
    24  
    25  	cmd := &cobra.Command{
    26  		Use:     "rm [OPTIONS] PLUGIN [PLUGIN...]",
    27  		Short:   "Remove one or more plugins",
    28  		Aliases: []string{"remove"},
    29  		Args:    cli.RequiresMinArgs(1),
    30  		RunE: func(cmd *cobra.Command, args []string) error {
    31  			opts.plugins = args
    32  			return runRemove(dockerCli, &opts)
    33  		},
    34  	}
    35  
    36  	flags := cmd.Flags()
    37  	flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of an active plugin")
    38  	return cmd
    39  }
    40  
    41  func runRemove(dockerCli *command.DockerCli, opts *rmOptions) error {
    42  	ctx := context.Background()
    43  
    44  	var errs cli.Errors
    45  	for _, name := range opts.plugins {
    46  		named, err := reference.ParseNamed(name) // FIXME: validate
    47  		if err != nil {
    48  			return err
    49  		}
    50  		if reference.IsNameOnly(named) {
    51  			named = reference.WithDefaultTag(named)
    52  		}
    53  		ref, ok := named.(reference.NamedTagged)
    54  		if !ok {
    55  			return fmt.Errorf("invalid name: %s", named.String())
    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  }