Skip to content

Commit 41c2786

Browse files
authored
Merge pull request #5663 from Benehiko/ctx-jsonmessage
pkg/command: wrap `jsonmessage.DisplayJSONMessagesStream` with go context
2 parents fdb75d2 + 91adb70 commit 41c2786

14 files changed

Lines changed: 184 additions & 53 deletions

File tree

cli/command/container/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
"github.com/docker/cli/cli/command"
1414
"github.com/docker/cli/cli/command/completion"
1515
"github.com/docker/cli/cli/command/image"
16+
"github.com/docker/cli/cli/internal/jsonstream"
1617
"github.com/docker/cli/cli/streams"
1718
"github.com/docker/cli/opts"
1819
"github.com/docker/docker/api/types/container"
1920
imagetypes "github.com/docker/docker/api/types/image"
2021
"github.com/docker/docker/api/types/versions"
2122
"github.com/docker/docker/errdefs"
22-
"github.com/docker/docker/pkg/jsonmessage"
2323
specs "github.com/opencontainers/image-spec/specs-go/v1"
2424
"github.com/pkg/errors"
2525
"github.com/spf13/cobra"
@@ -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 jsonstream.Display(ctx, responseBody, out)
152152
}
153153

154154
type cidFile struct {

cli/command/container/run_test.go

Lines changed: 14 additions & 19 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")
@@ -253,19 +248,19 @@ func TestRunPullTermination(t *testing.T) {
253248
assert.NilError(t, server.Close(), "failed to close imageCreateFunc server")
254249
return
255250
default:
251+
assert.NilError(t, enc.Encode(jsonmessage.JSONMessage{
252+
Status: "Downloading",
253+
ID: fmt.Sprintf("id-%d", i),
254+
TimeNano: time.Now().UnixNano(),
255+
Time: time.Now().Unix(),
256+
Progress: &jsonmessage.JSONProgress{
257+
Current: int64(i),
258+
Total: 100,
259+
Start: 0,
260+
},
261+
}))
262+
time.Sleep(100 * time.Millisecond)
256263
}
257-
assert.NilError(t, enc.Encode(jsonmessage.JSONMessage{
258-
Status: "Downloading",
259-
ID: fmt.Sprintf("id-%d", i),
260-
TimeNano: time.Now().UnixNano(),
261-
Time: time.Now().Unix(),
262-
Progress: &jsonmessage.JSONProgress{
263-
Current: int64(i),
264-
Total: 100,
265-
Start: 0,
266-
},
267-
}))
268-
time.Sleep(100 * time.Millisecond)
269264
}
270265
}()
271266
attachCh <- struct{}{}
@@ -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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/docker/cli/cli/command"
2121
"github.com/docker/cli/cli/command/completion"
2222
"github.com/docker/cli/cli/command/image/build"
23+
"github.com/docker/cli/cli/internal/jsonstream"
24+
"github.com/docker/cli/cli/streams"
2325
"github.com/docker/cli/opts"
2426
"github.com/docker/docker/api"
2527
"github.com/docker/docker/api/types"
@@ -28,7 +30,6 @@ import (
2830
"github.com/docker/docker/builder/remotecontext/urlutil"
2931
"github.com/docker/docker/pkg/archive"
3032
"github.com/docker/docker/pkg/idtools"
31-
"github.com/docker/docker/pkg/jsonmessage"
3233
"github.com/docker/docker/pkg/progress"
3334
"github.com/docker/docker/pkg/streamformatter"
3435
"github.com/pkg/errors"
@@ -352,7 +353,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
352353
defer response.Body.Close()
353354

354355
imageID := ""
355-
aux := func(msg jsonmessage.JSONMessage) {
356+
aux := func(msg jsonstream.JSONMessage) {
356357
var result types.BuildResult
357358
if err := json.Unmarshal(*msg.Aux, &result); err != nil {
358359
fmt.Fprintf(dockerCli.Err(), "Failed to parse aux message: %s", err)
@@ -361,9 +362,9 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
361362
}
362363
}
363364

364-
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), aux)
365+
err = jsonstream.Display(ctx, response.Body, streams.NewOut(buildBuff), jsonstream.WithAuxCallback(aux))
365366
if err != nil {
366-
if jerr, ok := err.(*jsonmessage.JSONError); ok {
367+
if jerr, ok := err.(*jsonstream.JSONError); ok {
367368
// If no error code is set, default to 1
368369
if jerr.Code == 0 {
369370
jerr.Code = 1

cli/command/image/import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
11+
"github.com/docker/cli/cli/internal/jsonstream"
1112
dockeropts "github.com/docker/cli/opts"
1213
"github.com/docker/docker/api/types/image"
13-
"github.com/docker/docker/pkg/jsonmessage"
1414
"github.com/spf13/cobra"
1515
)
1616

@@ -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 jsonstream.Display(ctx, responseBody, dockerCli.Out())
9494
}

cli/command/image/load.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
11+
"github.com/docker/cli/cli/internal/jsonstream"
1112
"github.com/docker/docker/api/types/image"
12-
"github.com/docker/docker/pkg/jsonmessage"
1313
"github.com/moby/sys/sequential"
1414
"github.com/pkg/errors"
1515
"github.com/spf13/cobra"
@@ -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 jsonstream.Display(ctx, response.Body, dockerCli.Out())
9393
}
9494

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

cli/command/image/push.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"github.com/docker/cli/cli"
1616
"github.com/docker/cli/cli/command"
1717
"github.com/docker/cli/cli/command/completion"
18+
"github.com/docker/cli/cli/internal/jsonstream"
1819
"github.com/docker/cli/cli/streams"
1920
"github.com/docker/docker/api/types/auxprogress"
2021
"github.com/docker/docker/api/types/image"
2122
registrytypes "github.com/docker/docker/api/types/registry"
22-
"github.com/docker/docker/pkg/jsonmessage"
2323
"github.com/docker/docker/registry"
2424
"github.com/morikuni/aec"
2525
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -140,23 +140,23 @@ 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 = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux()))
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 jsonstream.Display(ctx, responseBody, dockerCli.Out(), jsonstream.WithAuxCallback(handleAux()))
154154
}
155155

156156
var notes []string
157157

158-
func handleAux(dockerCli command.Cli) func(jm jsonmessage.JSONMessage) {
159-
return func(jm jsonmessage.JSONMessage) {
158+
func handleAux() func(jm jsonstream.JSONMessage) {
159+
return func(jm jsonstream.JSONMessage) {
160160
b := []byte(*jm.Aux)
161161

162162
var stripped auxprogress.ManifestPushedInsteadOfIndex

cli/command/image/trust.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010

1111
"github.com/distribution/reference"
1212
"github.com/docker/cli/cli/command"
13+
"github.com/docker/cli/cli/internal/jsonstream"
1314
"github.com/docker/cli/cli/streams"
1415
"github.com/docker/cli/cli/trust"
1516
"github.com/docker/docker/api/types"
1617
"github.com/docker/docker/api/types/image"
1718
registrytypes "github.com/docker/docker/api/types/registry"
18-
"github.com/docker/docker/pkg/jsonmessage"
1919
"github.com/docker/docker/registry"
2020
"github.com/opencontainers/go-digest"
2121
"github.com/pkg/errors"
@@ -39,20 +39,20 @@ 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{}
5252
// Count the times of calling for handleTarget,
5353
// if it is called more that once, that should be considered an error in a trusted push.
5454
cnt := 0
55-
handleTarget := func(msg jsonmessage.JSONMessage) {
55+
handleTarget := func(msg jsonstream.JSONMessage) {
5656
cnt++
5757
if cnt > 1 {
5858
// handleTarget should only be called once. This will be treated as an error.
@@ -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 := jsonstream.Display(ctx, in, ioStreams.Out()); 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 := jsonstream.Display(ctx, in, ioStreams.Out(), jsonstream.WithAuxCallback(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 jsonstream.Display(ctx, responseBody, out)
287287
}
288288

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

cli/command/plugin/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"github.com/docker/cli/cli"
1010
"github.com/docker/cli/cli/command"
1111
"github.com/docker/cli/cli/command/image"
12+
"github.com/docker/cli/cli/internal/jsonstream"
1213
"github.com/docker/docker/api/types"
1314
registrytypes "github.com/docker/docker/api/types/registry"
14-
"github.com/docker/docker/pkg/jsonmessage"
1515
"github.com/docker/docker/registry"
1616
"github.com/pkg/errors"
1717
"github.com/spf13/cobra"
@@ -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 := jsonstream.Display(ctx, responseBody, dockerCli.Out()); 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
99
"github.com/docker/cli/cli/command/image"
10+
"github.com/docker/cli/cli/internal/jsonstream"
1011
registrytypes "github.com/docker/docker/api/types/registry"
11-
"github.com/docker/docker/pkg/jsonmessage"
1212
"github.com/docker/docker/registry"
1313
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
@@ -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 jsonstream.Display(ctx, responseBody, dockerCli.Out())
7373
}

cli/command/plugin/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/distribution/reference"
99
"github.com/docker/cli/cli"
1010
"github.com/docker/cli/cli/command"
11+
"github.com/docker/cli/cli/internal/jsonstream"
1112
"github.com/docker/docker/errdefs"
12-
"github.com/docker/docker/pkg/jsonmessage"
1313
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
1515
)
@@ -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 := jsonstream.Display(ctx, responseBody, dockerCli.Out()); 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)