From 95c0ade04bb8fe95029fed521126e3268a1eda7f Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 6 Dec 2013 11:55:56 -0800 Subject: [PATCH] fix jsonmessage in build --- buildfile.go | 4 ++-- commands.go | 2 -- utils/jsonmessage.go | 7 +++++-- utils/streamformatter.go | 12 ++++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/buildfile.go b/buildfile.go index 170d739ee4..adbf98d274 100644 --- a/buildfile.go +++ b/buildfile.go @@ -375,7 +375,7 @@ type StdoutFormater struct { } func (sf *StdoutFormater) Write(buf []byte) (int, error) { - formattedBuf := sf.StreamFormatter.FormatStatus("", "%s", string(buf)) + formattedBuf := sf.StreamFormatter.FormatStream(string(buf)) n, err := sf.Writer.Write(formattedBuf) if n != len(formattedBuf) { return n, io.ErrShortWrite @@ -389,7 +389,7 @@ type StderrFormater struct { } func (sf *StderrFormater) Write(buf []byte) (int, error) { - formattedBuf := sf.StreamFormatter.FormatStatus("", "%s", "\033[91m"+string(buf)+"\033[0m") + formattedBuf := sf.StreamFormatter.FormatStream("\033[91m" + string(buf) + "\033[0m") n, err := sf.Writer.Write(formattedBuf) if n != len(formattedBuf) { return n, io.ErrShortWrite diff --git a/commands.go b/commands.go index b7e7c08cac..48c0241b17 100644 --- a/commands.go +++ b/commands.go @@ -229,8 +229,6 @@ func (cli *DockerCli) CmdBuild(args ...string) error { if context != nil { headers.Set("Content-Type", "application/tar") } - // Temporary hack to fix displayJSON behavior - cli.isTerminal = false err = cli.stream("POST", fmt.Sprintf("/build?%s", v.Encode()), body, cli.out, headers) if jerr, ok := err.(*utils.JSONError); ok { return &utils.StatusError{Status: jerr.Message, StatusCode: jerr.Code} diff --git a/utils/jsonmessage.go b/utils/jsonmessage.go index c36ec27763..82fe631b49 100644 --- a/utils/jsonmessage.go +++ b/utils/jsonmessage.go @@ -66,6 +66,7 @@ func (p *JSONProgress) String() string { } type JSONMessage struct { + Stream string `json:"stream,omitempty"` Status string `json:"status,omitempty"` Progress *JSONProgress `json:"progressDetail,omitempty"` ProgressMessage string `json:"progress,omitempty"` //deprecated @@ -87,7 +88,7 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error { if isTerminal { // [2K = erase entire current line fmt.Fprintf(out, "%c[2K\r", 27) - endl = "\r\n" + endl = "\r" } if jm.Time != 0 { fmt.Fprintf(out, "[%s] ", time.Unix(jm.Time, 0)) @@ -102,8 +103,10 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error { fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl) } else if jm.ProgressMessage != "" { //deprecated fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl) + } else if jm.Stream != "" { + fmt.Fprintf(out, "%s%s", jm.Stream, endl) } else { - fmt.Fprintf(out, "%s%s", jm.Status, endl) + fmt.Fprintf(out, "%s%s\n", jm.Status, endl) } return nil } diff --git a/utils/streamformatter.go b/utils/streamformatter.go index 60863d2aa6..0c41d0bddd 100644 --- a/utils/streamformatter.go +++ b/utils/streamformatter.go @@ -14,6 +14,18 @@ func NewStreamFormatter(json bool) *StreamFormatter { return &StreamFormatter{json, false} } +func (sf *StreamFormatter) FormatStream(str string) []byte { + sf.used = true + if sf.json { + b, err := json.Marshal(&JSONMessage{Stream: str}) + if err != nil { + return sf.FormatError(err) + } + return b + } + return []byte(str + "\r") +} + func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte { sf.used = true str := fmt.Sprintf(format, a...)