@@ -3,6 +3,7 @@ package jsonstream
33import (
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 }
0 commit comments