remove.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. errs = append(errs, err)
  39. continue
  40. }
  41. if reference.IsNameOnly(named) {
  42. named = reference.WithDefaultTag(named)
  43. }
  44. ref, ok := named.(reference.NamedTagged)
  45. if !ok {
  46. errs = append(errs, fmt.Errorf("invalid name: %s", named.String()))
  47. continue
  48. }
  49. // TODO: pass names to api instead of making multiple api calls
  50. if err := dockerCli.Client().PluginRemove(ctx, ref.String(), types.PluginRemoveOptions{Force: opts.force}); err != nil {
  51. errs = append(errs, err)
  52. continue
  53. }
  54. fmt.Fprintln(dockerCli.Out(), name)
  55. }
  56. // Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value.
  57. if errs != nil {
  58. return errs
  59. }
  60. return nil
  61. }