disable.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. func newDisableCommand(dockerCli *command.DockerCli) *cobra.Command {
  12. var force bool
  13. cmd := &cobra.Command{
  14. Use: "disable PLUGIN",
  15. Short: "Disable a plugin",
  16. Args: cli.ExactArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. return runDisable(dockerCli, args[0], force)
  19. },
  20. }
  21. flags := cmd.Flags()
  22. flags.BoolVarP(&force, "force", "f", false, "Force the disable of an active plugin")
  23. return cmd
  24. }
  25. func runDisable(dockerCli *command.DockerCli, name string, force bool) error {
  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. if err := dockerCli.Client().PluginDisable(context.Background(), ref.String(), types.PluginDisableOptions{Force: force}); err != nil {
  38. return err
  39. }
  40. fmt.Fprintln(dockerCli.Out(), name)
  41. return nil
  42. }