github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/service/helpers.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/ioutil"
     7  
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/cli/cli/command/service/progress"
    10  	"github.com/docker/docker/pkg/jsonmessage"
    11  )
    12  
    13  // waitOnService waits for the service to converge. It outputs a progress bar,
    14  // if appropriate based on the CLI flags.
    15  func waitOnService(ctx context.Context, dockerCli command.Cli, serviceID string, quiet bool) error {
    16  	errChan := make(chan error, 1)
    17  	pipeReader, pipeWriter := io.Pipe()
    18  
    19  	go func() {
    20  		errChan <- progress.ServiceProgress(ctx, dockerCli.Client(), serviceID, pipeWriter)
    21  	}()
    22  
    23  	if quiet {
    24  		go io.Copy(ioutil.Discard, pipeReader)
    25  		return <-errChan
    26  	}
    27  
    28  	err := jsonmessage.DisplayJSONMessagesToStream(pipeReader, dockerCli.Out(), nil)
    29  	if err == nil {
    30  		err = <-errChan
    31  	}
    32  	return err
    33  }