Browse code

Remove remaining registry methods from DockerCLI.

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2016/09/10 04:38:00
Showing 13 changed files
... ...
@@ -85,7 +85,7 @@ func pullImage(ctx context.Context, dockerCli *command.DockerCli, image string,
85 85
 		return err
86 86
 	}
87 87
 
88
-	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
88
+	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
89 89
 	encodedAuth, err := command.EncodeAuthToBase64(authConfig)
90 90
 	if err != nil {
91 91
 		return err
... ...
@@ -73,8 +73,8 @@ func runPull(dockerCli *command.DockerCli, opts pullOptions) error {
73 73
 
74 74
 	ctx := context.Background()
75 75
 
76
-	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
77
-	requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
76
+	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
77
+	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "pull")
78 78
 
79 79
 	if command.IsTrusted() && !registryRef.HasDigest() {
80 80
 		// Check if tag is digest
... ...
@@ -44,8 +44,8 @@ func runPush(dockerCli *command.DockerCli, remote string) error {
44 44
 	ctx := context.Background()
45 45
 
46 46
 	// Resolve the Auth config relevant for this server
47
-	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
48
-	requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
47
+	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
48
+	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "push")
49 49
 
50 50
 	if command.IsTrusted() {
51 51
 		return trustedPush(ctx, dockerCli, repoInfo, ref, authConfig, requestPrivilege)
... ...
@@ -66,8 +66,8 @@ func runSearch(dockerCli *command.DockerCli, opts searchOptions) error {
66 66
 
67 67
 	ctx := context.Background()
68 68
 
69
-	authConfig := dockerCli.ResolveAuthConfig(ctx, indexInfo)
70
-	requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(indexInfo, "search")
69
+	authConfig := command.ResolveAuthConfig(ctx, dockerCli, indexInfo)
70
+	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
71 71
 
72 72
 	encodedAuth, err := command.EncodeAuthToBase64(authConfig)
73 73
 	if err != nil {
... ...
@@ -499,7 +499,7 @@ func TrustedReference(ctx context.Context, cli *command.DockerCli, ref reference
499 499
 	}
500 500
 
501 501
 	// Resolve the Auth config relevant for this server
502
-	authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
502
+	authConfig := command.ResolveAuthConfig(ctx, cli, repoInfo.Index)
503 503
 
504 504
 	notaryRepo, err := GetNotaryRepository(cli, repoInfo, authConfig, "pull")
505 505
 	if err != nil {
... ...
@@ -61,14 +61,14 @@ func runInstall(dockerCli *command.DockerCli, opts pluginOptions) error {
61 61
 		return err
62 62
 	}
63 63
 
64
-	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
64
+	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
65 65
 
66 66
 	encodedAuth, err := command.EncodeAuthToBase64(authConfig)
67 67
 	if err != nil {
68 68
 		return err
69 69
 	}
70 70
 
71
-	registryAuthFunc := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "plugin install")
71
+	registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "plugin install")
72 72
 
73 73
 	options := types.PluginInstallOptions{
74 74
 		RegistryAuth:          encodedAuth,
... ...
@@ -45,7 +45,7 @@ func runPush(dockerCli *command.DockerCli, name string) error {
45 45
 	if err != nil {
46 46
 		return err
47 47
 	}
48
-	authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
48
+	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
49 49
 
50 50
 	encodedAuth, err := command.EncodeAuthToBase64(authConfig)
51 51
 	if err != nil {
... ...
@@ -20,15 +20,14 @@ import (
20 20
 )
21 21
 
22 22
 // ElectAuthServer returns the default registry to use (by asking the daemon)
23
-// TODO: only used in registry package and from ResolveAuthConfig
24
-func (cli *DockerCli) ElectAuthServer(ctx context.Context) string {
23
+func ElectAuthServer(ctx context.Context, cli *DockerCli) string {
25 24
 	// The daemon `/info` endpoint informs us of the default registry being
26 25
 	// used. This is essential in cross-platforms environment, where for
27 26
 	// example a Linux client might be interacting with a Windows daemon, hence
28 27
 	// the default registry URL might be Windows specific.
29 28
 	serverAddress := registry.IndexServer
30
-	if info, err := cli.client.Info(ctx); err != nil {
31
-		fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
29
+	if info, err := cli.Client().Info(ctx); err != nil {
30
+		fmt.Fprintf(cli.Out(), "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
32 31
 	} else {
33 32
 		serverAddress = info.IndexServerAddress
34 33
 	}
... ...
@@ -46,13 +45,12 @@ func EncodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
46 46
 
47 47
 // RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
48 48
 // for the given command.
49
-// TODO: image/plugin
50
-func (cli *DockerCli) RegistryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
49
+func RegistryAuthenticationPrivilegedFunc(cli *DockerCli, index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
51 50
 	return func() (string, error) {
52
-		fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
51
+		fmt.Fprintf(cli.Out(), "\nPlease login prior to %s:\n", cmdName)
53 52
 		indexServer := registry.GetAuthConfigKey(index)
54
-		isDefaultRegistry := indexServer == cli.ElectAuthServer(context.Background())
55
-		authConfig, err := cli.ConfigureAuth("", "", indexServer, isDefaultRegistry)
53
+		isDefaultRegistry := indexServer == ElectAuthServer(context.Background(), cli)
54
+		authConfig, err := ConfigureAuth(cli, "", "", indexServer, isDefaultRegistry)
56 55
 		if err != nil {
57 56
 			return "", err
58 57
 		}
... ...
@@ -63,11 +61,10 @@ func (cli *DockerCli) RegistryAuthenticationPrivilegedFunc(index *registrytypes.
63 63
 // ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
64 64
 // default index, it uses the default index name for the daemon's platform,
65 65
 // not the client's platform.
66
-// TODO: plugin/image/container and from RetrieveAuthTokenFromImage
67
-func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
66
+func ResolveAuthConfig(ctx context.Context, cli *DockerCli, index *registrytypes.IndexInfo) types.AuthConfig {
68 67
 	configKey := index.Name
69 68
 	if index.Official {
70
-		configKey = cli.ElectAuthServer(ctx)
69
+		configKey = ElectAuthServer(ctx, cli)
71 70
 	}
72 71
 
73 72
 	a, _ := cli.CredentialsStore().Get(configKey)
... ...
@@ -75,8 +72,7 @@ func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytype
75 75
 }
76 76
 
77 77
 // ConfigureAuth returns an AuthConfig from the specified user, password and server.
78
-// TODO: only used in registry package
79
-func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, isDefaultRegistry bool) (types.AuthConfig, error) {
78
+func ConfigureAuth(cli *DockerCli, flUser, flPassword, serverAddress string, isDefaultRegistry bool) (types.AuthConfig, error) {
80 79
 	// On Windows, force the use of the regular OS stdin stream. Fixes #14336/#14210
81 80
 	if runtime.GOOS == "windows" {
82 81
 		cli.in = NewInStream(os.Stdin)
... ...
@@ -107,10 +103,10 @@ func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, is
107 107
 	if flUser = strings.TrimSpace(flUser); flUser == "" {
108 108
 		if isDefaultRegistry {
109 109
 			// if this is a default registry (docker hub), then display the following message.
110
-			fmt.Fprintln(cli.out, "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
110
+			fmt.Fprintln(cli.Out(), "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.")
111 111
 		}
112
-		cli.promptWithDefault("Username", authconfig.Username)
113
-		flUser = readInput(cli.in, cli.out)
112
+		promptWithDefault(cli.Out(), "Username", authconfig.Username)
113
+		flUser = readInput(cli.In(), cli.Out())
114 114
 		flUser = strings.TrimSpace(flUser)
115 115
 		if flUser == "" {
116 116
 			flUser = authconfig.Username
... ...
@@ -124,11 +120,11 @@ func (cli *DockerCli) ConfigureAuth(flUser, flPassword, serverAddress string, is
124 124
 		if err != nil {
125 125
 			return authconfig, err
126 126
 		}
127
-		fmt.Fprintf(cli.out, "Password: ")
127
+		fmt.Fprintf(cli.Out(), "Password: ")
128 128
 		term.DisableEcho(cli.In().FD(), oldState)
129 129
 
130
-		flPassword = readInput(cli.in, cli.out)
131
-		fmt.Fprint(cli.out, "\n")
130
+		flPassword = readInput(cli.In(), cli.Out())
131
+		fmt.Fprint(cli.Out(), "\n")
132 132
 
133 133
 		term.RestoreTerminal(cli.In().FD(), oldState)
134 134
 		if flPassword == "" {
... ...
@@ -154,19 +150,18 @@ func readInput(in io.Reader, out io.Writer) string {
154 154
 	return string(line)
155 155
 }
156 156
 
157
-func (cli *DockerCli) promptWithDefault(prompt string, configDefault string) {
157
+func promptWithDefault(out io.Writer, prompt string, configDefault string) {
158 158
 	if configDefault == "" {
159
-		fmt.Fprintf(cli.out, "%s: ", prompt)
159
+		fmt.Fprintf(out, "%s: ", prompt)
160 160
 	} else {
161
-		fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
161
+		fmt.Fprintf(out, "%s (%s): ", prompt, configDefault)
162 162
 	}
163 163
 }
164 164
 
165 165
 // RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
166
-// TODO: used in service/stack packages
167
-func (cli *DockerCli) RetrieveAuthTokenFromImage(ctx context.Context, image string) (string, error) {
166
+func RetrieveAuthTokenFromImage(ctx context.Context, cli *DockerCli, image string) (string, error) {
168 167
 	// Retrieve encoded auth token from the image reference
169
-	authConfig, err := cli.resolveAuthConfigFromImage(ctx, image)
168
+	authConfig, err := resolveAuthConfigFromImage(ctx, cli, image)
170 169
 	if err != nil {
171 170
 		return "", err
172 171
 	}
... ...
@@ -178,7 +173,7 @@ func (cli *DockerCli) RetrieveAuthTokenFromImage(ctx context.Context, image stri
178 178
 }
179 179
 
180 180
 // resolveAuthConfigFromImage retrieves that AuthConfig using the image string
181
-func (cli *DockerCli) resolveAuthConfigFromImage(ctx context.Context, image string) (types.AuthConfig, error) {
181
+func resolveAuthConfigFromImage(ctx context.Context, cli *DockerCli, image string) (types.AuthConfig, error) {
182 182
 	registryRef, err := reference.ParseNamed(image)
183 183
 	if err != nil {
184 184
 		return types.AuthConfig{}, err
... ...
@@ -187,6 +182,5 @@ func (cli *DockerCli) resolveAuthConfigFromImage(ctx context.Context, image stri
187 187
 	if err != nil {
188 188
 		return types.AuthConfig{}, err
189 189
 	}
190
-	authConfig := cli.ResolveAuthConfig(ctx, repoInfo.Index)
191
-	return authConfig, nil
190
+	return ResolveAuthConfig(ctx, cli, repoInfo.Index), nil
192 191
 }
... ...
@@ -52,7 +52,7 @@ func runLogin(dockerCli *command.DockerCli, opts loginOptions) error {
52 52
 
53 53
 	var (
54 54
 		serverAddress string
55
-		authServer    = dockerCli.ElectAuthServer(ctx)
55
+		authServer    = command.ElectAuthServer(ctx, dockerCli)
56 56
 	)
57 57
 	if opts.serverAddress != "" {
58 58
 		serverAddress = opts.serverAddress
... ...
@@ -62,7 +62,7 @@ func runLogin(dockerCli *command.DockerCli, opts loginOptions) error {
62 62
 
63 63
 	isDefaultRegistry := serverAddress == authServer
64 64
 
65
-	authConfig, err := dockerCli.ConfigureAuth(opts.user, opts.password, serverAddress, isDefaultRegistry)
65
+	authConfig, err := command.ConfigureAuth(dockerCli, opts.user, opts.password, serverAddress, isDefaultRegistry)
66 66
 	if err != nil {
67 67
 		return err
68 68
 	}
... ...
@@ -35,7 +35,7 @@ func runLogout(dockerCli *command.DockerCli, serverAddress string) error {
35 35
 	var isDefaultRegistry bool
36 36
 
37 37
 	if serverAddress == "" {
38
-		serverAddress = dockerCli.ElectAuthServer(ctx)
38
+		serverAddress = command.ElectAuthServer(ctx, dockerCli)
39 39
 		isDefaultRegistry = true
40 40
 	}
41 41
 
... ...
@@ -55,7 +55,7 @@ func runCreate(dockerCli *command.DockerCli, opts *serviceOptions) error {
55 55
 	// only send auth if flag was set
56 56
 	if opts.registryAuth {
57 57
 		// Retrieve encoded auth token from the image reference
58
-		encodedAuth, err := dockerCli.RetrieveAuthTokenFromImage(ctx, opts.image)
58
+		encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, opts.image)
59 59
 		if err != nil {
60 60
 			return err
61 61
 		}
... ...
@@ -82,7 +82,7 @@ func runUpdate(dockerCli *command.DockerCli, flags *pflag.FlagSet, serviceID str
82 82
 		// Retrieve encoded auth token from the image reference
83 83
 		// This would be the old image if it didn't change in this update
84 84
 		image := service.Spec.TaskTemplate.ContainerSpec.Image
85
-		encodedAuth, err := dockerCli.RetrieveAuthTokenFromImage(ctx, image)
85
+		encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, image)
86 86
 		if err != nil {
87 87
 			return err
88 88
 		}
... ...
@@ -197,7 +197,7 @@ func deployServices(
197 197
 		if sendAuth {
198 198
 			// Retrieve encoded auth token from the image reference
199 199
 			image := serviceSpec.TaskTemplate.ContainerSpec.Image
200
-			encodedAuth, err = dockerCli.RetrieveAuthTokenFromImage(ctx, image)
200
+			encodedAuth, err = command.RetrieveAuthTokenFromImage(ctx, dockerCli, image)
201 201
 			if err != nil {
202 202
 				return err
203 203
 			}