Alano Terblanche 91adb70d6b
pkg/command: wrap jsonmessage.DisplayJSONMessagesStream with go context
Allows for the `jsonmessage.DisplayJSONMessagesStream` function
to correctly return when the context is cancelled with the appropriate
reason (`ctx.Error()`) instead of just a nil error.

Follow-up to 30a73ff19c

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
Co-authored-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-01-20 16:36:11 +01:00

33 lines
769 B
Go

package service
import (
"context"
"io"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/service/progress"
"github.com/docker/cli/cli/internal/jsonstream"
)
// WaitOnService waits for the service to converge. It outputs a progress bar,
// if appropriate based on the CLI flags.
func WaitOnService(ctx context.Context, dockerCli command.Cli, serviceID string, quiet bool) error {
errChan := make(chan error, 1)
pipeReader, pipeWriter := io.Pipe()
go func() {
errChan <- progress.ServiceProgress(ctx, dockerCli.Client(), serviceID, pipeWriter)
}()
if quiet {
go io.Copy(io.Discard, pipeReader)
return <-errChan
}
err := jsonstream.Display(ctx, pipeReader, dockerCli.Out())
if err == nil {
err = <-errChan
}
return err
}