From 87678e5e71d8ce6a922f0bf840662e24e7fcef2f Mon Sep 17 00:00:00 2001 From: David Gageot Date: Sun, 20 Jan 2019 18:48:31 -0500 Subject: [PATCH] Fix coloured output when building in // Signed-off-by: David Gageot --- pkg/skaffold/build/parallel.go | 24 ++++++++++++++++-------- pkg/skaffold/color/formatter.go | 9 +++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/pkg/skaffold/build/parallel.go b/pkg/skaffold/build/parallel.go index bf150d47450..87406fbbe2d 100644 --- a/pkg/skaffold/build/parallel.go +++ b/pkg/skaffold/build/parallel.go @@ -44,28 +44,35 @@ func InParallel(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts n := len(artifacts) tags := make([]string, n) errs := make([]error, n) - outputs := make([]chan (string), n) + outputs := make([]chan []byte, n) // Run builds in // for index := range artifacts { i := index - lines := make(chan (string), bufferedLinesPerArtifact) + lines := make(chan []byte, bufferedLinesPerArtifact) outputs[i] = lines r, w := io.Pipe() + // Log to the pipe, output will be collected and printed later go func() { - // Log to the pipe, output will be collected and printed later - fmt.Fprintf(w, "Building [%s]...\n", artifacts[i].ImageName) + // Make sure logs are printed in colors + var cw io.WriteCloser + if color.IsTerminal(out) { + cw = color.ColoredWriteCloser{WriteCloser: w} + } else { + cw = w + } - tags[i], errs[i] = buildArtifact(ctx, w, tagger, artifacts[i]) - w.Close() + color.Default.Fprintf(cw, "Building [%s]...\n", artifacts[i].ImageName) + tags[i], errs[i] = buildArtifact(ctx, cw, tagger, artifacts[i]) + cw.Close() }() go func() { scanner := bufio.NewScanner(r) for scanner.Scan() { - lines <- scanner.Text() + lines <- scanner.Bytes() } close(lines) }() @@ -76,7 +83,8 @@ func InParallel(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts for i, artifact := range artifacts { for line := range outputs[i] { - color.Default.Fprintln(out, line) + out.Write(line) + fmt.Fprintln(out) } if errs[i] != nil { diff --git a/pkg/skaffold/color/formatter.go b/pkg/skaffold/color/formatter.go index 21f5a6f1a3a..e82ea0fba23 100644 --- a/pkg/skaffold/color/formatter.go +++ b/pkg/skaffold/color/formatter.go @@ -94,9 +94,18 @@ func (c Color) Fprintf(out io.Writer, format string, a ...interface{}) (n int, e return fmt.Fprintf(out, format, a...) } +// ColoredWriteCloser forces printing with colors to an io.WriteCloser. +type ColoredWriteCloser struct { + io.WriteCloser +} + // This implementation comes from logrus (https://github.com/sirupsen/logrus/blob/master/terminal_check_notappengine.go), // unfortunately logrus doesn't expose a public interface we can use to call it. func isTerminal(w io.Writer) bool { + if _, ok := w.(ColoredWriteCloser); ok { + return true + } + switch v := w.(type) { case *os.File: return terminal.IsTerminal(int(v.Fd()))