watchErrorStream watches the errorStream for remote command error data, decodes it with the given errorStreamDecoder, sends the decoded error (or nil if the remote command exited successfully) to the returned error channel, and closes it. This function returns immediately.
(errorStream io.Reader, d errorStreamDecoder)
| 34 | // command exited successfully) to the returned error channel, and closes it. |
| 35 | // This function returns immediately. |
| 36 | func watchErrorStream(errorStream io.Reader, d errorStreamDecoder) chan error { |
| 37 | errorChan := make(chan error) |
| 38 | |
| 39 | go func() { |
| 40 | defer runtime.HandleCrash() |
| 41 | |
| 42 | message, err := ioutil.ReadAll(errorStream) |
| 43 | switch { |
| 44 | case err != nil && err != io.EOF: |
| 45 | errorChan <- fmt.Errorf("error reading from error stream: %s", err) |
| 46 | case len(message) > 0: |
| 47 | errorChan <- d.decode(message) |
| 48 | default: |
| 49 | errorChan <- nil |
| 50 | } |
| 51 | close(errorChan) |
| 52 | }() |
| 53 | |
| 54 | return errorChan |
| 55 | } |