enable.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 enableOpts struct {
  12. timeout int
  13. name string
  14. }
  15. func newEnableCommand(dockerCli *command.DockerCli) *cobra.Command {
  16. var opts enableOpts
  17. cmd := &cobra.Command{
  18. Use: "enable [OPTIONS] PLUGIN",
  19. Short: "Enable a plugin",
  20. Args: cli.ExactArgs(1),
  21. RunE: func(cmd *cobra.Command, args []string) error {
  22. opts.name = args[0]
  23. return runEnable(dockerCli, &opts)
  24. },
  25. }
  26. flags := cmd.Flags()
  27. flags.IntVar(&opts.timeout, "timeout", 0, "HTTP client timeout (in seconds)")
  28. return cmd
  29. }
  30. func runEnable(dockerCli *command.DockerCli, opts *enableOpts) error {
  31. name := opts.name
  32. named, err := reference.ParseNamed(name) // FIXME: validate
  33. if err != nil {
  34. return err
  35. }
  36. if reference.IsNameOnly(named) {
  37. named = reference.WithDefaultTag(named)
  38. }
  39. ref, ok := named.(reference.NamedTagged)
  40. if !ok {
  41. return fmt.Errorf("invalid name: %s", named.String())
  42. }
  43. if opts.timeout < 0 {
  44. return fmt.Errorf("negative timeout %d is invalid", opts.timeout)
  45. }
  46. if err := dockerCli.Client().PluginEnable(context.Background(), ref.String(), types.PluginEnableOptions{Timeout: opts.timeout}); err != nil {
  47. return err
  48. }
  49. fmt.Fprintln(dockerCli.Out(), name)
  50. return nil
  51. }