enable.go 1.1 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/pkg/errors"
  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. if opts.timeout < 0 {
  33. return errors.Errorf("negative timeout %d is invalid", opts.timeout)
  34. }
  35. if err := dockerCli.Client().PluginEnable(context.Background(), name, types.PluginEnableOptions{Timeout: opts.timeout}); err != nil {
  36. return err
  37. }
  38. fmt.Fprintln(dockerCli.Out(), name)
  39. return nil
  40. }