Skip to content

Commit 4884112

Browse files
committed
pkg/command: pass context along to jsonmessage.DisplayJSONMessagesStream
Allows for the `jsonmessage.DisplayJSONMessagesStream` to correctly return when the context is cancelled with the appropriate error message, instead of just a nil error. Follow-up to 30a73ff Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
1 parent 5afa739 commit 4884112

12 files changed

Lines changed: 20 additions & 25 deletions

File tree

cli/command/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func pullImage(ctx context.Context, dockerCli command.Cli, img string, options *
148148
if options.quiet {
149149
out = streams.NewOut(io.Discard)
150150
}
151-
return jsonmessage.DisplayJSONMessagesToStream(responseBody, out, nil)
151+
return jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, out, nil)
152152
}
153153

154154
type cidFile struct {

cli/command/container/run_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,7 @@ func TestRunPullTermination(t *testing.T) {
230230
createContainerFunc: func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig,
231231
platform *specs.Platform, containerName string,
232232
) (container.CreateResponse, error) {
233-
select {
234-
case <-ctx.Done():
235-
return container.CreateResponse{}, ctx.Err()
236-
default:
237-
}
238-
return container.CreateResponse{}, fakeNotFound{}
233+
return container.CreateResponse{}, errors.New("shouldn't try to create a container")
239234
},
240235
containerAttachFunc: func(ctx context.Context, containerID string, options container.AttachOptions) (types.HijackedResponse, error) {
241236
return types.HijackedResponse{}, errors.New("shouldn't try to attach to a container")
@@ -277,7 +272,7 @@ func TestRunPullTermination(t *testing.T) {
277272
cmd := NewRunCommand(fakeCLI)
278273
cmd.SetOut(io.Discard)
279274
cmd.SetErr(io.Discard)
280-
cmd.SetArgs([]string{"foobar:latest"})
275+
cmd.SetArgs([]string{"--pull", "always", "foobar:latest"})
281276

282277
cmdErrC := make(chan error, 1)
283278
go func() {

cli/command/image/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
361361
}
362362
}
363363

364-
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), aux)
364+
err = jsonmessage.DisplayJSONMessagesStream(ctx, response.Body, buildBuff, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), aux)
365365
if err != nil {
366366
if jerr, ok := err.(*jsonmessage.JSONError); ok {
367367
// If no error code is set, default to 1

cli/command/image/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ func runImport(ctx context.Context, dockerCli command.Cli, options importOptions
9090
}
9191
defer responseBody.Close()
9292

93-
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
93+
return jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, dockerCli.Out(), nil)
9494
}

cli/command/image/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func runLoad(ctx context.Context, dockerCli command.Cli, opts loadOptions) error
8989
defer response.Body.Close()
9090

9191
if response.Body != nil && response.JSON {
92-
return jsonmessage.DisplayJSONMessagesToStream(response.Body, dockerCli.Out(), nil)
92+
return jsonmessage.DisplayJSONMessagesToStream(ctx, response.Body, dockerCli.Out(), nil)
9393
}
9494

9595
_, err = io.Copy(dockerCli.Out(), response.Body)

cli/command/image/push.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,17 @@ To push the complete multi-platform image, remove the --platform flag.
140140
defer responseBody.Close()
141141
if !opts.untrusted {
142142
// TODO PushTrustedReference currently doesn't respect `--quiet`
143-
return PushTrustedReference(dockerCli, repoInfo, ref, authConfig, responseBody)
143+
return PushTrustedReference(ctx, dockerCli, repoInfo, ref, authConfig, responseBody)
144144
}
145145

146146
if opts.quiet {
147-
err = jsonmessage.DisplayJSONMessagesToStream(responseBody, streams.NewOut(io.Discard), handleAux(dockerCli))
147+
err = jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, streams.NewOut(io.Discard), handleAux(dockerCli))
148148
if err == nil {
149149
fmt.Fprintln(dockerCli.Out(), ref.String())
150150
}
151151
return err
152152
}
153-
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), handleAux(dockerCli))
153+
return jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, dockerCli.Out(), handleAux(dockerCli))
154154
}
155155

156156
var notes []string

cli/command/image/trust.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func TrustedPush(ctx context.Context, cli command.Cli, repoInfo *registry.Reposi
3939

4040
defer responseBody.Close()
4141

42-
return PushTrustedReference(cli, repoInfo, ref, authConfig, responseBody)
42+
return PushTrustedReference(ctx, cli, repoInfo, ref, authConfig, responseBody)
4343
}
4444

4545
// PushTrustedReference pushes a canonical reference to the trust server.
4646
//
4747
//nolint:gocyclo
48-
func PushTrustedReference(ioStreams command.Streams, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader) error {
48+
func PushTrustedReference(ctx context.Context, ioStreams command.Streams, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader) error {
4949
// If it is a trusted push we would like to find the target entry which match the
5050
// tag provided in the function and then do an AddTarget later.
5151
target := &client.Target{}
@@ -84,14 +84,14 @@ func PushTrustedReference(ioStreams command.Streams, repoInfo *registry.Reposito
8484
default:
8585
// We want trust signatures to always take an explicit tag,
8686
// otherwise it will act as an untrusted push.
87-
if err := jsonmessage.DisplayJSONMessagesToStream(in, ioStreams.Out(), nil); err != nil {
87+
if err := jsonmessage.DisplayJSONMessagesToStream(ctx, in, ioStreams.Out(), nil); err != nil {
8888
return err
8989
}
9090
fmt.Fprintln(ioStreams.Err(), "No tag specified, skipping trust metadata push")
9191
return nil
9292
}
9393

94-
if err := jsonmessage.DisplayJSONMessagesToStream(in, ioStreams.Out(), handleTarget); err != nil {
94+
if err := jsonmessage.DisplayJSONMessagesToStream(ctx, in, ioStreams.Out(), handleTarget); err != nil {
9595
return err
9696
}
9797

@@ -283,7 +283,7 @@ func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth tru
283283
if opts.quiet {
284284
out = streams.NewOut(io.Discard)
285285
}
286-
return jsonmessage.DisplayJSONMessagesToStream(responseBody, out, nil)
286+
return jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, out, nil)
287287
}
288288

289289
// TrustedReference returns the canonical trusted reference for an image reference

cli/command/plugin/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func runInstall(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
129129
return err
130130
}
131131
defer responseBody.Close()
132-
if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
132+
if err := jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, dockerCli.Out(), nil); err != nil {
133133
return err
134134
}
135135
fmt.Fprintf(dockerCli.Out(), "Installed plugin %s\n", opts.remote) // todo: return proper values from the API for this result

cli/command/plugin/push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func runPush(ctx context.Context, dockerCli command.Cli, opts pushOptions) error
6666
defer responseBody.Close()
6767

6868
if !opts.untrusted {
69-
return image.PushTrustedReference(dockerCli, repoInfo, named, authConfig, responseBody)
69+
return image.PushTrustedReference(ctx, dockerCli, repoInfo, named, authConfig, responseBody)
7070
}
7171

72-
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
72+
return jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, dockerCli.Out(), nil)
7373
}

cli/command/plugin/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
8686
return err
8787
}
8888
defer responseBody.Close()
89-
if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
89+
if err := jsonmessage.DisplayJSONMessagesToStream(ctx, responseBody, dockerCli.Out(), nil); err != nil {
9090
return err
9191
}
9292
fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result

0 commit comments

Comments
 (0)