notable changes:
- api: remove deprecated NoBaseImageSpecifier
- api/stdcopy: move to api/pkg/stdcopy
- api/types/container: add aliases for go-connections/nat types
- pkg/progress: move to api/pkg/progress
- pkg/jsonmessage: move JSONError to api/types/jsonstream
- pkg/jsonmessage: move JSONProgress to api/types/jsonstream
- pkg/jsonmessage: move to client/pkg/jsonmessage
- pkg/jsonmessage: remove github.com/morikuni/aec dependency
- pkg/jsonmessage: stop printing deprecated progressDetail, errorDetail,
remove DisplayJSONMessagesToStream and Stream interface
- pkg/streamformatter: move to api/pkg/streamformatter
- pkg/streamformatter: split from pkg/jsonmessage
full diff: 2574c2b2e9...4faedf2bec
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package jsonstream
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/docker/cli/cli/streams"
|
|
"github.com/moby/moby/api/types/jsonstream"
|
|
"github.com/moby/moby/client/pkg/jsonmessage"
|
|
)
|
|
|
|
type (
|
|
JSONError = jsonstream.Error
|
|
JSONMessage = jsonmessage.JSONMessage
|
|
JSONProgress = jsonmessage.JSONProgress
|
|
)
|
|
|
|
type ctxReader struct {
|
|
err chan error
|
|
r io.Reader
|
|
}
|
|
|
|
func (r *ctxReader) Read(p []byte) (n int, err error) {
|
|
select {
|
|
case err = <-r.err:
|
|
return 0, err
|
|
default:
|
|
return r.r.Read(p)
|
|
}
|
|
}
|
|
|
|
type Options func(*options)
|
|
|
|
type options struct {
|
|
AuxCallback func(JSONMessage)
|
|
}
|
|
|
|
func WithAuxCallback(cb func(JSONMessage)) Options {
|
|
return func(o *options) {
|
|
o.AuxCallback = cb
|
|
}
|
|
}
|
|
|
|
// Display prints the JSON messages from the given reader to the given stream.
|
|
//
|
|
// It wraps the [jsonmessage.DisplayJSONMessagesStream] function to make it
|
|
// "context aware" and appropriately returns why the function was canceled.
|
|
//
|
|
// It returns an error if the context is canceled, but not if the input reader / stream is closed.
|
|
func Display(ctx context.Context, in io.Reader, stream *streams.Out, opts ...Options) error {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
|
|
reader := &ctxReader{err: make(chan error, 1), r: in}
|
|
stopFunc := context.AfterFunc(ctx, func() { reader.err <- ctx.Err() })
|
|
defer stopFunc()
|
|
|
|
o := options{}
|
|
for _, opt := range opts {
|
|
opt(&o)
|
|
}
|
|
|
|
if err := jsonmessage.DisplayJSONMessagesStream(reader, stream, stream.FD(), stream.IsTerminal(), o.AuxCallback); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ctx.Err()
|
|
}
|