| 69 | } |
| 70 | |
| 71 | func streamToChannel(ready chan<- struct{}, |
| 72 | stop <-chan struct{}, done chan<- struct{}, |
| 73 | stream io.Reader, lines chan<- string, |
| 74 | ) { |
| 75 | defer close(done) |
| 76 | close(ready) |
| 77 | scanner := bufio.NewScanner(stream) |
| 78 | lineBuffer := make([]byte, bufio.MaxScanTokenSize) // 64KB |
| 79 | const maxCapacity = 20 * 1024 * 1024 // 20MB |
| 80 | scanner.Buffer(lineBuffer, maxCapacity) |
| 81 | |
| 82 | for scanner.Scan() { |
| 83 | // scanner is closed if the context is canceled |
| 84 | // or if the command failed starting because the |
| 85 | // stream is closed (io.EOF error). |
| 86 | lines <- scanner.Text() |
| 87 | } |
| 88 | err := scanner.Err() |
| 89 | if err == nil || errors.Is(err, os.ErrClosed) { |
| 90 | return |
| 91 | } |
| 92 | |
| 93 | // ignore the error if it is stopped. |
| 94 | select { |
| 95 | case <-stop: |
| 96 | return |
| 97 | default: |
| 98 | lines <- "stream error: " + err.Error() |
| 99 | } |
| 100 | } |