remove.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/spf13/cobra"
  8. "golang.org/x/net/context"
  9. )
  10. type rmOptions struct {
  11. force bool
  12. plugins []string
  13. }
  14. func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
  15. var opts rmOptions
  16. cmd := &cobra.Command{
  17. Use: "rm [OPTIONS] PLUGIN [PLUGIN...]",
  18. Short: "Remove one or more plugins",
  19. Aliases: []string{"remove"},
  20. Args: cli.RequiresMinArgs(1),
  21. RunE: func(cmd *cobra.Command, args []string) error {
  22. opts.plugins = args
  23. return runRemove(dockerCli, &opts)
  24. },
  25. }
  26. flags := cmd.Flags()
  27. flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of an active plugin")
  28. return cmd
  29. }
  30. func runRemove(dockerCli *command.DockerCli, opts *rmOptions) error {
  31. ctx := context.Background()
  32. var errs cli.Errors
  33. for _, name := range opts.plugins {
  34. // TODO: pass names to api instead of making multiple api calls
  35. if err := dockerCli.Client().PluginRemove(ctx, name, types.PluginRemoveOptions{Force: opts.force}); err != nil {
  36. errs = append(errs, err)
  37. continue
  38. }
  39. fmt.Fprintln(dockerCli.Out(), name)
  40. }
  41. // Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value.
  42. if errs != nil {
  43. return errs
  44. }
  45. return nil
  46. }