sg: stop buffering command output until newlines (#30672)

This commit is contained in:
Thorsten Ball 2022-02-04 11:31:44 +01:00 committed by GitHub
parent af21a04b16
commit 5c3df12c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -135,7 +135,7 @@ func startCmd(ctx context.Context, dir string, cmd Command, globalEnv map[string
stderrWriter = io.MultiWriter(logger, sc.stderrBuf)
}
wg, err := process.PipeOutput(ctx, sc.Cmd, stdoutWriter, stderrWriter)
wg, err := process.PipeOutputUnbuffered(ctx, sc.Cmd, stdoutWriter, stderrWriter)
if err != nil {
return nil, err
}

View File

@ -17,6 +17,24 @@ import (
//
// See this issue for more details: https://github.com/golang/go/issues/21922
func PipeOutput(ctx context.Context, c *exec.Cmd, stdoutWriter, stderrWriter io.Writer) (*sync.WaitGroup, error) {
return pipeOutputWithCopy(ctx, c, stdoutWriter, stderrWriter, func(w io.Writer, r io.Reader) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fmt.Fprintln(w, scanner.Text())
}
})
}
func PipeOutputUnbuffered(ctx context.Context, c *exec.Cmd, stdoutWriter, stderrWriter io.Writer) (*sync.WaitGroup, error) {
return pipeOutputWithCopy(ctx, c, stdoutWriter, stderrWriter, func(w io.Writer, r io.Reader) {
_, err := io.Copy(w, r)
if err != nil {
panic(fmt.Sprintf("failed to pipe output: %s", err))
}
})
}
func pipeOutputWithCopy(ctx context.Context, c *exec.Cmd, stdoutWriter, stderrWriter io.Writer, fn copyFunc) (*sync.WaitGroup, error) {
stdoutPipe, err := c.StdoutPipe()
if err != nil {
return nil, err
@ -42,10 +60,7 @@ func PipeOutput(ctx context.Context, c *exec.Cmd, stdoutWriter, stderrWriter io.
readIntoBuf := func(w io.Writer, r io.Reader) {
defer wg.Done()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fmt.Fprintln(w, scanner.Text())
}
fn(w, r)
}
wg.Add(2)
@ -54,3 +69,5 @@ func PipeOutput(ctx context.Context, c *exec.Cmd, stdoutWriter, stderrWriter io.
return wg, nil
}
type copyFunc func(w io.Writer, r io.Reader)