From e1793ab9fc3d172a416183ef3df20daf089eba19 Mon Sep 17 00:00:00 2001 From: Andrew Meyer Date: Thu, 28 Mar 2019 16:36:45 -0500 Subject: [PATCH] Colorize docker output Signed-off-by: Andrew Meyer --- docker/docker.go | 29 ++++++++++++++++++++++++++++- style/style.go | 5 +++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/docker/docker.go b/docker/docker.go index 0cc45bf67..61c698759 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -16,6 +16,8 @@ import ( "github.com/docker/docker/pkg/term" "github.com/google/go-containerregistry/pkg/authn" "github.com/pkg/errors" + + "github.com/buildpack/pack/style" ) type Client struct { @@ -82,7 +84,7 @@ func (d *Client) PullImage(ctx context.Context, imageID string, stdout io.Writer } termFd, isTerm := term.GetFdInfo(stdout) - err = jsonmessage.DisplayJSONMessagesStream(rc, stdout, termFd, isTerm, nil) + err = jsonmessage.DisplayJSONMessagesStream(rc, &colorizedWriter{stdout}, termFd, isTerm, nil) if err != nil { return err } @@ -114,3 +116,28 @@ func (d *Client) registryAuth(ref string) (string, error) { } return regAuth, nil } + +type colorizedWriter struct { + writer io.Writer +} + +type colorFunc = func(string, ...interface{}) string + +func (w *colorizedWriter) Write(p []byte) (n int, err error) { + msg := string(p) + colorizers := map[string]colorFunc{ + "Waiting": style.Waiting, + "Pulling fs layer": style.Waiting, + "Downloading": style.Working, + "Download complete": style.Working, + "Extracting": style.Working, + "Pull complete": style.Complete, + "Already exists": style.Complete, + "=": style.ProgressBar, + ">": style.ProgressBar, + } + for pattern, colorize := range colorizers { + msg = strings.Replace(msg, pattern, colorize(pattern), -1) + } + return w.writer.Write([]byte(msg)) +} diff --git a/style/style.go b/style/style.go index 5a5952a4f..56ba6ac25 100644 --- a/style/style.go +++ b/style/style.go @@ -30,3 +30,8 @@ var Step = func(format string, a ...interface{}) string { var Prefix = color.CyanString var TimestampColorCode = color.FgHiBlack + +var Waiting = color.HiBlackString +var Working = color.HiBlueString +var Complete = color.GreenString +var ProgressBar = color.HiBlueString