cli/command/container: pullImage: use DisplayJSONMessagesToStream utility

This utility provides the same logic as was implemented here (and using it
aligns with the "docker pull" equivalent).

Also added a TODO to replace this function with the regular "docker pull"
code.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-04-11 23:52:40 +02:00
parent b9b98aee5d
commit 5d856a5d91
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 10 additions and 13 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/image" "github.com/docker/cli/cli/command/image"
"github.com/docker/cli/cli/streams"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
@ -111,7 +112,8 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptio
return nil return nil
} }
func pullImage(ctx context.Context, dockerCli command.Cli, image string, platform string, out io.Writer) error { // FIXME(thaJeztah): this is the only code-path that uses APIClient.ImageCreate. Rewrite this to use the regular "pull" code (or vice-versa).
func pullImage(ctx context.Context, dockerCli command.Cli, image string, opts *createOptions) error {
encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, image) encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, image)
if err != nil { if err != nil {
return err return err
@ -119,19 +121,18 @@ func pullImage(ctx context.Context, dockerCli command.Cli, image string, platfor
responseBody, err := dockerCli.Client().ImageCreate(ctx, image, types.ImageCreateOptions{ responseBody, err := dockerCli.Client().ImageCreate(ctx, image, types.ImageCreateOptions{
RegistryAuth: encodedAuth, RegistryAuth: encodedAuth,
Platform: platform, Platform: opts.platform,
}) })
if err != nil { if err != nil {
return err return err
} }
defer responseBody.Close() defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesStream( out := dockerCli.Err()
responseBody, if opts.quiet {
out, out = io.Discard
dockerCli.Out().FD(), }
dockerCli.Out().IsTerminal(), return jsonmessage.DisplayJSONMessagesToStream(responseBody, streams.NewOut(out), nil)
nil)
} }
type cidFile struct { type cidFile struct {
@ -221,11 +222,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
} }
pullAndTagImage := func() error { pullAndTagImage := func() error {
pullOut := dockerCli.Err() if err := pullImage(ctx, dockerCli, config.Image, opts); err != nil {
if opts.quiet {
pullOut = io.Discard
}
if err := pullImage(ctx, dockerCli, config.Image, opts.platform, pullOut); err != nil {
return err return err
} }
if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil { if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil {