Skip to content

Commit

Permalink
Disable all colors in Buildpacks’s output when not in a terminal
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Feb 6, 2020
1 parent 11d005d commit 6cbfb76
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ require (
github.com/krishicks/yaml-patch v0.0.10
github.com/markbates/inflect v1.0.4 // indirect
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/mattn/go-colorable v0.1.4
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/buildkit v0.6.3
github.com/onsi/ginkgo v1.10.3 // indirect
Expand Down
8 changes: 0 additions & 8 deletions pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ import (
"strings"

"github.com/buildpacks/pack"
"github.com/heroku/color"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

// For testing
Expand Down Expand Up @@ -85,12 +83,6 @@ func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact,
}

func runPackBuild(ctx context.Context, out io.Writer, localDocker docker.LocalDaemon, opts pack.BuildOptions) error {
// If out is not a terminal, let's make sure pack doesn't output with colors.
if _, isTerm := util.IsTerminal(out); !isTerm {
// pack uses heroku/color under the hood.
color.Disable(true)
}

packClient, err := pack.NewClient(
pack.WithDockerClient(localDocker.RawClient()),
pack.WithLogger(NewLogger(out)),
Expand Down
20 changes: 19 additions & 1 deletion pkg/skaffold/build/buildpacks/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"io"

"github.com/buildpacks/pack/logging"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

// logger exists to meet the requirements of the pack logger.
Expand All @@ -30,12 +33,27 @@ type logger struct {
}

func NewLogger(out io.Writer) logging.Logger {
// If out is not a terminal, let's make sure no colors are printed.
if _, isTerm := util.IsTerminal(out); !isTerm {
out = colorable.NewNonColorable(out)
}

l := logrus.New()
l.SetOutput(out)
l.SetFormatter(new(plainFormatter))

return &logger{
Logger: logrus.StandardLogger(),
Logger: l,
out: out,
}
}

type plainFormatter struct{}

func (f *plainFormatter) Format(entry *logrus.Entry) ([]byte, error) {
return []byte(entry.Message + "\n"), nil
}

func (l *logger) Debug(msg string) {
l.Logger.Debug(msg)
}
Expand Down

0 comments on commit 6cbfb76

Please sign in to comment.