Skip to content

Commit a9c1664

Browse files
authored
fix: synchronize terraform log output, fix init coloring (#2482)
1 parent 54a585d commit a9c1664

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

provisioner/terraform/executor.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"runtime"
1414
"strings"
15+
"sync"
1516

1617
"golang.org/x/xerrors"
1718

@@ -53,9 +54,14 @@ func (e executor) execWriteOutput(ctx context.Context, args, env []string, stdOu
5354
// #nosec
5455
cmd := exec.CommandContext(ctx, e.binaryPath, args...)
5556
cmd.Dir = e.workdir
56-
cmd.Stdout = stdOutWriter
57-
cmd.Stderr = stdErrWriter
5857
cmd.Env = env
58+
59+
// We want logs to be written in the correct order, so we wrap all logging
60+
// in a sync.Mutex.
61+
mut := &sync.Mutex{}
62+
cmd.Stdout = syncWriter{mut, stdOutWriter}
63+
cmd.Stderr = syncWriter{mut, stdErrWriter}
64+
5965
return cmd.Run()
6066
}
6167

@@ -115,12 +121,17 @@ func (e executor) version(ctx context.Context) (*version.Version, error) {
115121
func (e executor) init(ctx context.Context, logr logger) error {
116122
outWriter, doneOut := logWriter(logr, proto.LogLevel_DEBUG)
117123
errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR)
118-
119124
defer func() {
120125
<-doneOut
121126
<-doneErr
122127
}()
123-
return e.execWriteOutput(ctx, []string{"init"}, e.basicEnv(), outWriter, errWriter)
128+
129+
args := []string{
130+
"init",
131+
"-no-color",
132+
"-input=false",
133+
}
134+
return e.execWriteOutput(ctx, args, e.basicEnv(), outWriter, errWriter)
124135
}
125136

126137
// revive:disable-next-line:flag-parameter
@@ -389,3 +400,16 @@ type terraformProvisionLogDiagnostic struct {
389400
Summary string `json:"summary"`
390401
Detail string `json:"detail"`
391402
}
403+
404+
// syncWriter wraps an io.Writer in a sync.Mutex.
405+
type syncWriter struct {
406+
mut *sync.Mutex
407+
w io.Writer
408+
}
409+
410+
// Write implements io.Writer.
411+
func (sw syncWriter) Write(p []byte) (n int, err error) {
412+
sw.mut.Lock()
413+
defer sw.mut.Unlock()
414+
return sw.w.Write(p)
415+
}

0 commit comments

Comments
 (0)