remove.go 1.7 KB

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