install.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/docker/docker/registry"
  9. "github.com/spf13/cobra"
  10. "golang.org/x/net/context"
  11. )
  12. type pluginOptions struct {
  13. name string
  14. grantPerms bool
  15. }
  16. func newInstallCommand(dockerCli *client.DockerCli) *cobra.Command {
  17. var options pluginOptions
  18. cmd := &cobra.Command{
  19. Use: "install",
  20. Short: "Install a plugin",
  21. Args: cli.RequiresMinArgs(1), // TODO: allow for set args
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. options.name = args[0]
  24. return runInstall(dockerCli, options)
  25. },
  26. }
  27. flags := cmd.Flags()
  28. flags.BoolVar(&options.grantPerms, "grant-all-permissions", true, "grant all permissions necessary to run the plugin")
  29. return cmd
  30. }
  31. func runInstall(dockerCli *client.DockerCli, options pluginOptions) error {
  32. named, err := reference.ParseNamed(options.name) // FIXME: validate
  33. if err != nil {
  34. return err
  35. }
  36. named = reference.WithDefaultTag(named)
  37. ref, ok := named.(reference.NamedTagged)
  38. if !ok {
  39. return fmt.Errorf("invalid name: %s", named.String())
  40. }
  41. ctx := context.Background()
  42. repoInfo, err := registry.ParseRepositoryInfo(named)
  43. authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
  44. encodedAuth, err := client.EncodeAuthToBase64(authConfig)
  45. if err != nil {
  46. return err
  47. }
  48. // TODO: pass noEnable flag
  49. return dockerCli.Client().PluginInstall(ctx, ref.String(), encodedAuth, options.grantPerms, false, dockerCli.In(), dockerCli.Out())
  50. }