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