Skip to content

Commit

Permalink
Colorize docker output
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Meyer <ameyer@pivotal.io>
  • Loading branch information
ameyer-pivotal committed Apr 2, 2019
1 parent 943a663 commit e1793ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 28 additions & 1 deletion docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}
5 changes: 5 additions & 0 deletions style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e1793ab

Please sign in to comment.