remove.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // +build experimental
  2. package plugin
  3. import (
  4. "fmt"
  5. "github.com/docker/docker/api/client"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/reference"
  8. "github.com/spf13/cobra"
  9. "golang.org/x/net/context"
  10. )
  11. func newRemoveCommand(dockerCli *client.DockerCli) *cobra.Command {
  12. cmd := &cobra.Command{
  13. Use: "rm PLUGIN",
  14. Short: "Remove a plugin",
  15. Aliases: []string{"remove"},
  16. Args: cli.ExactArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. return runRemove(dockerCli, args)
  19. },
  20. }
  21. return cmd
  22. }
  23. func runRemove(dockerCli *client.DockerCli, names []string) error {
  24. var errs cli.Errors
  25. for _, name := range names {
  26. named, err := reference.ParseNamed(name) // FIXME: validate
  27. if err != nil {
  28. return err
  29. }
  30. if reference.IsNameOnly(named) {
  31. named = reference.WithDefaultTag(named)
  32. }
  33. ref, ok := named.(reference.NamedTagged)
  34. if !ok {
  35. return fmt.Errorf("invalid name: %s", named.String())
  36. }
  37. // TODO: pass names to api instead of making multiple api calls
  38. if err := dockerCli.Client().PluginRemove(context.Background(), ref.String()); err != nil {
  39. errs = append(errs, err)
  40. continue
  41. }
  42. fmt.Fprintln(dockerCli.Out(), name)
  43. }
  44. // Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value.
  45. if errs != nil {
  46. return errs
  47. }
  48. return nil
  49. }