Skip to content

Commit 3512338

Browse files
committed
DNM: testing
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 8eedbdc commit 3512338

File tree

10 files changed

+39
-10
lines changed

10 files changed

+39
-10
lines changed

cli/command/image/build.go

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

365-
err = jsonstream.Display(ctx, response.Body, streams.NewOut(buildBuff), jsonstream.WithAuxCallback(aux))
365+
err = jsonstream.DisplayStream(ctx, response.Body, streams.NewOut(buildBuff), jsonstream.WithAuxCallback(aux))
366366
if err != nil {
367367
var jerr *jsonstream.JSONError
368368
if errors.As(err, &jerr) {

cli/command/image/import.go

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

107-
return jsonstream.Display(ctx, responseBody, dockerCli.Out())
107+
return jsonstream.DisplayStream(ctx, responseBody, dockerCli.Out())
108108
}

cli/command/image/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ func runLoad(ctx context.Context, dockerCli command.Cli, opts loadOptions) error
9797
}
9898
defer func() { _ = res.Close() }()
9999

100-
return jsonstream.Display(ctx, res, dockerCli.Out())
100+
return jsonstream.DisplayStream(ctx, res, dockerCli.Out())
101101
}

cli/command/plugin/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func runInstall(ctx context.Context, dockerCLI command.Cli, opts pluginOptions)
100100
defer func() {
101101
_ = responseBody.Close()
102102
}()
103-
if err := jsonstream.Display(ctx, responseBody, dockerCLI.Out()); err != nil {
103+
if err := jsonstream.DisplayStream(ctx, responseBody, dockerCLI.Out()); err != nil {
104104
return err
105105
}
106106
_, _ = fmt.Fprintln(dockerCLI.Out(), "Installed plugin", opts.remote) // todo: return proper values from the API for this result

cli/command/plugin/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ func runPush(ctx context.Context, dockerCli command.Cli, name string) error {
5656
defer func() {
5757
_ = responseBody.Close()
5858
}()
59-
return jsonstream.Display(ctx, responseBody, dockerCli.Out())
59+
return jsonstream.DisplayStream(ctx, responseBody, dockerCli.Out())
6060
}

cli/command/plugin/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func runUpgrade(ctx context.Context, dockerCLI command.Cli, opts pluginOptions)
9090
defer func() {
9191
_ = responseBody.Close()
9292
}()
93-
if err := jsonstream.Display(ctx, responseBody, dockerCLI.Out()); err != nil {
93+
if err := jsonstream.DisplayStream(ctx, responseBody, dockerCLI.Out()); err != nil {
9494
return err
9595
}
9696
_, _ = fmt.Fprintf(dockerCLI.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result

cli/command/service/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func WaitOnService(ctx context.Context, dockerCli command.Cli, serviceID string,
2424
return <-errChan
2525
}
2626

27-
err := jsonstream.Display(ctx, pipeReader, dockerCli.Out())
27+
err := jsonstream.DisplayStream(ctx, pipeReader, dockerCli.Out())
2828
if err == nil {
2929
err = <-errChan
3030
}

cli/command/swarm/ca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func attach(ctx context.Context, dockerCLI command.Cli, opts caOptions) error {
124124
return <-errChan
125125
}
126126

127-
err := jsonstream.Display(ctx, pipeReader, dockerCLI.Out())
127+
err := jsonstream.DisplayStream(ctx, pipeReader, dockerCLI.Out())
128128
if err == nil {
129129
err = <-errChan
130130
}

internal/jsonstream/display.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jsonstream
33
import (
44
"context"
55
"io"
6+
"iter"
67

78
"github.com/docker/cli/cli/streams"
89
"github.com/moby/moby/api/types/jsonstream"
@@ -41,13 +42,41 @@ func WithAuxCallback(cb func(JSONMessage)) Options {
4142
}
4243
}
4344

45+
type ProgressResponse interface {
46+
io.ReadCloser
47+
JSONMessages(ctx context.Context) iter.Seq2[jsonstream.Message, error]
48+
}
49+
4450
// Display prints the JSON messages from the given reader to the given stream.
4551
//
52+
// It wraps the [jsonmessage.DisplayJSONMessages] function to make it
53+
// "context aware" and appropriately returns why the function was canceled.
54+
//
55+
// It returns an error if the context is canceled, but not if the input reader / stream is closed.
56+
func Display(ctx context.Context, in ProgressResponse, stream *streams.Out, opts ...Options) error {
57+
if ctx.Err() != nil {
58+
return ctx.Err()
59+
}
60+
61+
// reader := &ctxReader{err: make(chan error, 1), r: in}
62+
// stopFunc := context.AfterFunc(ctx, func() { reader.err <- ctx.Err() })
63+
// defer stopFunc()
64+
//
65+
o := options{}
66+
for _, opt := range opts {
67+
opt(&o)
68+
}
69+
70+
return jsonmessage.DisplayJSONMessages(in.JSONMessages(ctx), stream, stream.FD(), stream.IsTerminal(), o.AuxCallback)
71+
}
72+
73+
// DisplayStream prints the JSON messages from the given reader to the given stream.
74+
//
4675
// It wraps the [jsonmessage.DisplayJSONMessagesStream] function to make it
4776
// "context aware" and appropriately returns why the function was canceled.
4877
//
4978
// It returns an error if the context is canceled, but not if the input reader / stream is closed.
50-
func Display(ctx context.Context, in io.Reader, stream *streams.Out, opts ...Options) error {
79+
func DisplayStream(ctx context.Context, in io.Reader, stream *streams.Out, opts ...Options) error {
5180
if ctx.Err() != nil {
5281
return ctx.Err()
5382
}

internal/jsonstream/display_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestDisplay(t *testing.T) {
3636

3737
done := make(chan error)
3838
go func() {
39-
done <- Display(streamCtx, client, streams.NewOut(io.Discard))
39+
done <- DisplayStream(streamCtx, client, streams.NewOut(io.Discard))
4040
}()
4141

4242
cancelStream()

0 commit comments

Comments
 (0)