Skip to content

Commit 5d856a5

Browse files
committed
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>
1 parent b9b98ae commit 5d856a5

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

cli/command/container/create.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/docker/cli/cli/command"
1313
"github.com/docker/cli/cli/command/completion"
1414
"github.com/docker/cli/cli/command/image"
15+
"github.com/docker/cli/cli/streams"
1516
"github.com/docker/cli/opts"
1617
"github.com/docker/distribution/reference"
1718
"github.com/docker/docker/api/types"
@@ -111,27 +112,27 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptio
111112
return nil
112113
}
113114

114-
func pullImage(ctx context.Context, dockerCli command.Cli, image string, platform string, out io.Writer) error {
115+
// FIXME(thaJeztah): this is the only code-path that uses APIClient.ImageCreate. Rewrite this to use the regular "pull" code (or vice-versa).
116+
func pullImage(ctx context.Context, dockerCli command.Cli, image string, opts *createOptions) error {
115117
encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, image)
116118
if err != nil {
117119
return err
118120
}
119121

120122
responseBody, err := dockerCli.Client().ImageCreate(ctx, image, types.ImageCreateOptions{
121123
RegistryAuth: encodedAuth,
122-
Platform: platform,
124+
Platform: opts.platform,
123125
})
124126
if err != nil {
125127
return err
126128
}
127129
defer responseBody.Close()
128130

129-
return jsonmessage.DisplayJSONMessagesStream(
130-
responseBody,
131-
out,
132-
dockerCli.Out().FD(),
133-
dockerCli.Out().IsTerminal(),
134-
nil)
131+
out := dockerCli.Err()
132+
if opts.quiet {
133+
out = io.Discard
134+
}
135+
return jsonmessage.DisplayJSONMessagesToStream(responseBody, streams.NewOut(out), nil)
135136
}
136137

137138
type cidFile struct {
@@ -221,11 +222,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
221222
}
222223

223224
pullAndTagImage := func() error {
224-
pullOut := dockerCli.Err()
225-
if opts.quiet {
226-
pullOut = io.Discard
227-
}
228-
if err := pullImage(ctx, dockerCli, config.Image, opts.platform, pullOut); err != nil {
225+
if err := pullImage(ctx, dockerCli, config.Image, opts); err != nil {
229226
return err
230227
}
231228
if taggedRef, ok := namedRef.(reference.NamedTagged); ok && trustedRef != nil {

0 commit comments

Comments
 (0)