mirror of
https://github.com/coder/coder.git
synced 2026-10-28 02:42:54 +08:00
fix: synchronize terraform log output, fix init coloring (#2482)
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
@@ -53,9 +54,14 @@ func (e executor) execWriteOutput(ctx context.Context, args, env []string, stdOu
|
|||||||
// #nosec
|
// #nosec
|
||||||
cmd := exec.CommandContext(ctx, e.binaryPath, args...)
|
cmd := exec.CommandContext(ctx, e.binaryPath, args...)
|
||||||
cmd.Dir = e.workdir
|
cmd.Dir = e.workdir
|
||||||
cmd.Stdout = stdOutWriter
|
|
||||||
cmd.Stderr = stdErrWriter
|
|
||||||
cmd.Env = env
|
cmd.Env = env
|
||||||
|
|
||||||
|
// We want logs to be written in the correct order, so we wrap all logging
|
||||||
|
// in a sync.Mutex.
|
||||||
|
mut := &sync.Mutex{}
|
||||||
|
cmd.Stdout = syncWriter{mut, stdOutWriter}
|
||||||
|
cmd.Stderr = syncWriter{mut, stdErrWriter}
|
||||||
|
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,12 +121,17 @@ func (e executor) version(ctx context.Context) (*version.Version, error) {
|
|||||||
func (e executor) init(ctx context.Context, logr logger) error {
|
func (e executor) init(ctx context.Context, logr logger) error {
|
||||||
outWriter, doneOut := logWriter(logr, proto.LogLevel_DEBUG)
|
outWriter, doneOut := logWriter(logr, proto.LogLevel_DEBUG)
|
||||||
errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR)
|
errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
<-doneOut
|
<-doneOut
|
||||||
<-doneErr
|
<-doneErr
|
||||||
}()
|
}()
|
||||||
return e.execWriteOutput(ctx, []string{"init"}, e.basicEnv(), outWriter, errWriter)
|
|
||||||
|
args := []string{
|
||||||
|
"init",
|
||||||
|
"-no-color",
|
||||||
|
"-input=false",
|
||||||
|
}
|
||||||
|
return e.execWriteOutput(ctx, args, e.basicEnv(), outWriter, errWriter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// revive:disable-next-line:flag-parameter
|
// revive:disable-next-line:flag-parameter
|
||||||
@@ -389,3 +400,16 @@ type terraformProvisionLogDiagnostic struct {
|
|||||||
Summary string `json:"summary"`
|
Summary string `json:"summary"`
|
||||||
Detail string `json:"detail"`
|
Detail string `json:"detail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// syncWriter wraps an io.Writer in a sync.Mutex.
|
||||||
|
type syncWriter struct {
|
||||||
|
mut *sync.Mutex
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write implements io.Writer.
|
||||||
|
func (sw syncWriter) Write(p []byte) (n int, err error) {
|
||||||
|
sw.mut.Lock()
|
||||||
|
defer sw.mut.Unlock()
|
||||||
|
return sw.w.Write(p)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user