From 0daba02a56abbab5534ce3fea2645800f863d9db Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 6 Feb 2020 10:44:03 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Disable=20all=20colors=20in=20Buildpacks?= =?UTF-8?q?=E2=80=99s=20output=20when=20not=20in=20a=20terminal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Gageot --- go.mod | 2 +- go.sum | 2 -- pkg/skaffold/build/buildpacks/lifecycle.go | 8 -------- pkg/skaffold/build/buildpacks/logger.go | 20 +++++++++++++++++++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 7d9e513b942..98ec0f215ec 100644 --- a/go.mod +++ b/go.mod @@ -63,7 +63,6 @@ require ( github.com/gophercloud/gophercloud v0.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway v1.12.1 github.com/hashicorp/golang-lru v0.5.3 // indirect - github.com/heroku/color v0.0.6 github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c // indirect github.com/imdario/mergo v0.3.8 github.com/jstemmer/go-junit-report v0.9.1 // indirect @@ -71,6 +70,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 diff --git a/go.sum b/go.sum index 5a405f34731..c9974284ae6 100644 --- a/go.sum +++ b/go.sum @@ -111,8 +111,6 @@ github.com/buildpacks/imgutil v0.0.0-20200127205531-eec2000815b1 h1:1+v3XHezNuXC github.com/buildpacks/imgutil v0.0.0-20200127205531-eec2000815b1/go.mod h1:E3lXJcNXcRefJQAHW5rqboonet+jtOml4qImbJhYGAo= github.com/buildpacks/lifecycle v0.5.1-0.20191217221752-3b74c943b7b3 h1:15zYyFmNbrtx5MmyVEKVgY2/eearqEqGJXLCjmvUo58= github.com/buildpacks/lifecycle v0.5.1-0.20191217221752-3b74c943b7b3/go.mod h1:ZIuIs9B6tjAW4dthhltKVyEUlhRfFWWix9eqoInMGX4= -github.com/buildpacks/pack v0.8.1-0.20200128153201-c4636375b6a7 h1:fVBzTLxaLqrERBNPQuATKEr+nZskEain+OHtzQ4kb3c= -github.com/buildpacks/pack v0.8.1-0.20200128153201-c4636375b6a7/go.mod h1:7obZCatYJVAQOmYCinSsKw/lfJhf3lrx4n0Z88uxVj0= github.com/buildpacks/pack v0.8.2-0.20200131162247-1a6e03a48c94 h1:QwGqY47lEgvCDSFNSD3EnAa7zny0AFvvCNgEX/qipCc= github.com/buildpacks/pack v0.8.2-0.20200131162247-1a6e03a48c94/go.mod h1:hcHY0ZiDxwa6oxMPDE+gng+aJHJjUnnYfAwuonvu/Wc= github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk= diff --git a/pkg/skaffold/build/buildpacks/lifecycle.go b/pkg/skaffold/build/buildpacks/lifecycle.go index 1f17f252526..4dc152f3cd5 100644 --- a/pkg/skaffold/build/buildpacks/lifecycle.go +++ b/pkg/skaffold/build/buildpacks/lifecycle.go @@ -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 @@ -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)), diff --git a/pkg/skaffold/build/buildpacks/logger.go b/pkg/skaffold/build/buildpacks/logger.go index 576ebea3188..394bcd83088 100644 --- a/pkg/skaffold/build/buildpacks/logger.go +++ b/pkg/skaffold/build/buildpacks/logger.go @@ -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. @@ -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) } From 69044eba2b0a508ac947bdfc396ca9dcd82d38dd Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 6 Feb 2020 21:28:03 +0100 Subject: [PATCH 2/2] Format Signed-off-by: David Gageot --- pkg/skaffold/build/buildpacks/logger.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/skaffold/build/buildpacks/logger.go b/pkg/skaffold/build/buildpacks/logger.go index 394bcd83088..e816818dae5 100644 --- a/pkg/skaffold/build/buildpacks/logger.go +++ b/pkg/skaffold/build/buildpacks/logger.go @@ -40,6 +40,8 @@ func NewLogger(out io.Writer) logging.Logger { l := logrus.New() l.SetOutput(out) + + // By default, logrus prefixes lines with 'INFO[XXX]'. l.SetFormatter(new(plainFormatter)) return &logger{