github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/plugin/install.go (about) 1 // +build experimental 2 3 package plugin 4 5 import ( 6 "bufio" 7 "fmt" 8 "strings" 9 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/cli" 12 "github.com/docker/docker/cli/command" 13 "github.com/docker/docker/reference" 14 "github.com/docker/docker/registry" 15 "github.com/spf13/cobra" 16 "golang.org/x/net/context" 17 ) 18 19 type pluginOptions struct { 20 name string 21 grantPerms bool 22 disable bool 23 } 24 25 func newInstallCommand(dockerCli *command.DockerCli) *cobra.Command { 26 var options pluginOptions 27 cmd := &cobra.Command{ 28 Use: "install [OPTIONS] PLUGIN", 29 Short: "Install a plugin", 30 Args: cli.ExactArgs(1), // TODO: allow for set args 31 RunE: func(cmd *cobra.Command, args []string) error { 32 options.name = args[0] 33 return runInstall(dockerCli, options) 34 }, 35 } 36 37 flags := cmd.Flags() 38 flags.BoolVar(&options.grantPerms, "grant-all-permissions", false, "Grant all permissions necessary to run the plugin") 39 flags.BoolVar(&options.disable, "disable", false, "Do not enable the plugin on install") 40 41 return cmd 42 } 43 44 func runInstall(dockerCli *command.DockerCli, opts pluginOptions) error { 45 named, err := reference.ParseNamed(opts.name) // FIXME: validate 46 if err != nil { 47 return err 48 } 49 if reference.IsNameOnly(named) { 50 named = reference.WithDefaultTag(named) 51 } 52 ref, ok := named.(reference.NamedTagged) 53 if !ok { 54 return fmt.Errorf("invalid name: %s", named.String()) 55 } 56 57 ctx := context.Background() 58 59 repoInfo, err := registry.ParseRepositoryInfo(named) 60 if err != nil { 61 return err 62 } 63 64 authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index) 65 66 encodedAuth, err := command.EncodeAuthToBase64(authConfig) 67 if err != nil { 68 return err 69 } 70 71 registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "plugin install") 72 73 options := types.PluginInstallOptions{ 74 RegistryAuth: encodedAuth, 75 Disabled: opts.disable, 76 AcceptAllPermissions: opts.grantPerms, 77 AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name), 78 // TODO: Rename PrivilegeFunc, it has nothing to do with privileges 79 PrivilegeFunc: registryAuthFunc, 80 } 81 if err := dockerCli.Client().PluginInstall(ctx, ref.String(), options); err != nil { 82 return err 83 } 84 fmt.Fprintln(dockerCli.Out(), opts.name) 85 return nil 86 } 87 88 func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) { 89 return func(privileges types.PluginPrivileges) (bool, error) { 90 fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name) 91 for _, privilege := range privileges { 92 fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value) 93 } 94 95 fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ") 96 reader := bufio.NewReader(dockerCli.In()) 97 line, _, err := reader.ReadLine() 98 if err != nil { 99 return false, err 100 } 101 return strings.ToLower(string(line)) == "y", nil 102 } 103 }