Skip to content

Commit

Permalink
Fix coloured output when building in //
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jan 20, 2019
1 parent 51bcca6 commit 3fc58ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
24 changes: 16 additions & 8 deletions pkg/skaffold/build/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/color/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down

0 comments on commit 3fc58ef

Please sign in to comment.