enable.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 enableOpts struct {
  11. timeout int
  12. name string
  13. }
  14. func newEnableCommand(dockerCli *command.DockerCli) *cobra.Command {
  15. var opts enableOpts
  16. cmd := &cobra.Command{
  17. Use: "enable [OPTIONS] PLUGIN",
  18. Short: "Enable a plugin",
  19. Args: cli.ExactArgs(1),
  20. RunE: func(cmd *cobra.Command, args []string) error {
  21. opts.name = args[0]
  22. return runEnable(dockerCli, &opts)
  23. },
  24. }
  25. flags := cmd.Flags()
  26. flags.IntVar(&opts.timeout, "timeout", 0, "HTTP client timeout (in seconds)")
  27. return cmd
  28. }
  29. func runEnable(dockerCli *command.DockerCli, opts *enableOpts) error {
  30. name := opts.name
  31. if opts.timeout < 0 {
  32. return fmt.Errorf("negative timeout %d is invalid", opts.timeout)
  33. }
  34. if err := dockerCli.Client().PluginEnable(context.Background(), name, types.PluginEnableOptions{Timeout: opts.timeout}); err != nil {
  35. return err
  36. }
  37. fmt.Fprintln(dockerCli.Out(), name)
  38. return nil
  39. }