remove.go 1.7 KB

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