helpers.go 922 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package service
  2. import (
  3. "io"
  4. "github.com/docker/docker/cli/command"
  5. "github.com/docker/docker/cli/command/service/progress"
  6. "github.com/docker/docker/pkg/jsonmessage"
  7. "golang.org/x/net/context"
  8. )
  9. // waitOnService waits for the service to converge. It outputs a progress bar,
  10. // if appopriate based on the CLI flags.
  11. func waitOnService(ctx context.Context, dockerCli *command.DockerCli, serviceID string, opts *serviceOptions) error {
  12. errChan := make(chan error, 1)
  13. pipeReader, pipeWriter := io.Pipe()
  14. go func() {
  15. errChan <- progress.ServiceProgress(ctx, dockerCli.Client(), serviceID, pipeWriter)
  16. }()
  17. if opts.quiet {
  18. go func() {
  19. for {
  20. var buf [1024]byte
  21. if _, err := pipeReader.Read(buf[:]); err != nil {
  22. return
  23. }
  24. }
  25. }()
  26. return <-errChan
  27. }
  28. err := jsonmessage.DisplayJSONMessagesToStream(pipeReader, dockerCli.Out(), nil)
  29. if err == nil {
  30. err = <-errChan
  31. }
  32. return err
  33. }