2013-11-28 15:16:57 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-02-24 03:51:46 -05:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2014-03-11 13:40:06 -04:00
|
|
|
"io"
|
2013-11-28 15:16:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StreamFormatter struct {
|
|
|
|
json bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStreamFormatter(json bool) *StreamFormatter {
|
2014-06-02 06:41:47 -04:00
|
|
|
return &StreamFormatter{json}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
2014-02-21 01:42:31 -05:00
|
|
|
const streamNewline = "\r\n"
|
|
|
|
|
|
|
|
var streamNewlineBytes = []byte(streamNewline)
|
|
|
|
|
2013-12-06 14:55:56 -05:00
|
|
|
func (sf *StreamFormatter) FormatStream(str string) []byte {
|
|
|
|
if sf.json {
|
|
|
|
b, err := json.Marshal(&JSONMessage{Stream: str})
|
|
|
|
if err != nil {
|
|
|
|
return sf.FormatError(err)
|
|
|
|
}
|
2014-02-21 01:42:31 -05:00
|
|
|
return append(b, streamNewlineBytes...)
|
2013-12-06 14:55:56 -05:00
|
|
|
}
|
|
|
|
return []byte(str + "\r")
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:16:57 -05:00
|
|
|
func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte {
|
|
|
|
str := fmt.Sprintf(format, a...)
|
|
|
|
if sf.json {
|
|
|
|
b, err := json.Marshal(&JSONMessage{ID: id, Status: str})
|
|
|
|
if err != nil {
|
|
|
|
return sf.FormatError(err)
|
|
|
|
}
|
2014-02-21 01:42:31 -05:00
|
|
|
return append(b, streamNewlineBytes...)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2014-02-21 01:42:31 -05:00
|
|
|
return []byte(str + streamNewline)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *StreamFormatter) FormatError(err error) []byte {
|
|
|
|
if sf.json {
|
|
|
|
jsonError, ok := err.(*JSONError)
|
|
|
|
if !ok {
|
|
|
|
jsonError = &JSONError{Message: err.Error()}
|
|
|
|
}
|
|
|
|
if b, err := json.Marshal(&JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
|
2014-02-21 01:42:31 -05:00
|
|
|
return append(b, streamNewlineBytes...)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2014-02-21 01:42:31 -05:00
|
|
|
return []byte("{\"error\":\"format error\"}" + streamNewline)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2014-02-21 01:42:31 -05:00
|
|
|
return []byte("Error: " + err.Error() + streamNewline)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2015-02-24 03:51:46 -05:00
|
|
|
func (sf *StreamFormatter) FormatProg(id, action string, p interface{}) []byte {
|
|
|
|
switch progress := p.(type) {
|
|
|
|
case *JSONProgress:
|
|
|
|
return sf.FormatProgress(id, action, progress)
|
|
|
|
case progressreader.PR_JSONProgress:
|
|
|
|
return sf.FormatProgress(id, action, &JSONProgress{Current: progress.GetCurrent(), Total: progress.GetTotal()})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-28 15:16:57 -05:00
|
|
|
func (sf *StreamFormatter) FormatProgress(id, action string, progress *JSONProgress) []byte {
|
|
|
|
if progress == nil {
|
|
|
|
progress = &JSONProgress{}
|
|
|
|
}
|
|
|
|
if sf.json {
|
|
|
|
|
|
|
|
b, err := json.Marshal(&JSONMessage{
|
|
|
|
Status: action,
|
|
|
|
ProgressMessage: progress.String(),
|
|
|
|
Progress: progress,
|
|
|
|
ID: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2013-11-28 17:40:17 -05:00
|
|
|
endl := "\r"
|
|
|
|
if progress.String() == "" {
|
|
|
|
endl += "\n"
|
|
|
|
}
|
|
|
|
return []byte(action + " " + progress.String() + endl)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-22 16:35:35 -05:00
|
|
|
func (sf *StreamFormatter) Json() bool {
|
|
|
|
return sf.json
|
|
|
|
}
|
2014-03-11 13:40:06 -04:00
|
|
|
|
|
|
|
type StdoutFormater struct {
|
|
|
|
io.Writer
|
|
|
|
*StreamFormatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *StdoutFormater) Write(buf []byte) (int, error) {
|
|
|
|
formattedBuf := sf.StreamFormatter.FormatStream(string(buf))
|
|
|
|
n, err := sf.Writer.Write(formattedBuf)
|
|
|
|
if n != len(formattedBuf) {
|
|
|
|
return n, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return len(buf), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type StderrFormater struct {
|
|
|
|
io.Writer
|
|
|
|
*StreamFormatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *StderrFormater) Write(buf []byte) (int, error) {
|
|
|
|
formattedBuf := sf.StreamFormatter.FormatStream("\033[91m" + string(buf) + "\033[0m")
|
|
|
|
n, err := sf.Writer.Write(formattedBuf)
|
|
|
|
if n != len(formattedBuf) {
|
|
|
|
return n, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return len(buf), err
|
|
|
|
}
|