1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/streamformatter/streamformatter.go

124 lines
3.3 KiB
Go
Raw Normal View History

// Package streamformatter provides helper functions to format a stream.
package streamformatter
2013-11-28 15:16:57 -05:00
import (
"encoding/json"
"fmt"
"io"
"github.com/docker/docker/pkg/jsonmessage"
2013-11-28 15:16:57 -05:00
)
// StreamFormatter formats a stream, optionally using JSON.
2013-11-28 15:16:57 -05:00
type StreamFormatter struct {
json bool
}
// NewStreamFormatter returns a simple StreamFormatter
func NewStreamFormatter() *StreamFormatter {
return &StreamFormatter{}
}
// NewJSONStreamFormatter returns a StreamFormatter configured to stream json
func NewJSONStreamFormatter() *StreamFormatter {
return &StreamFormatter{true}
2013-11-28 15:16:57 -05:00
}
const streamNewline = "\r\n"
var streamNewlineBytes = []byte(streamNewline)
// FormatStream formats the specified stream.
2013-12-06 14:55:56 -05:00
func (sf *StreamFormatter) FormatStream(str string) []byte {
if sf.json {
b, err := json.Marshal(&jsonmessage.JSONMessage{Stream: str})
2013-12-06 14:55:56 -05:00
if err != nil {
return sf.FormatError(err)
}
return append(b, streamNewlineBytes...)
2013-12-06 14:55:56 -05:00
}
return []byte(str + "\r")
}
// FormatStatus formats the specified objects according to the specified format (and id).
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.JSONMessage{ID: id, Status: str})
2013-11-28 15:16:57 -05:00
if err != nil {
return sf.FormatError(err)
}
return append(b, streamNewlineBytes...)
2013-11-28 15:16:57 -05:00
}
return []byte(str + streamNewline)
2013-11-28 15:16:57 -05:00
}
// FormatError formats the specifed error.
2013-11-28 15:16:57 -05:00
func (sf *StreamFormatter) FormatError(err error) []byte {
if sf.json {
jsonError, ok := err.(*jsonmessage.JSONError)
2013-11-28 15:16:57 -05:00
if !ok {
jsonError = &jsonmessage.JSONError{Message: err.Error()}
2013-11-28 15:16:57 -05:00
}
if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
return append(b, streamNewlineBytes...)
2013-11-28 15:16:57 -05:00
}
return []byte("{\"error\":\"format error\"}" + streamNewline)
2013-11-28 15:16:57 -05:00
}
return []byte("Error: " + err.Error() + streamNewline)
2013-11-28 15:16:57 -05:00
}
// FormatProgress formats the progress information for a specified action.
func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessage.JSONProgress) []byte {
2013-11-28 15:16:57 -05:00
if progress == nil {
progress = &jsonmessage.JSONProgress{}
2013-11-28 15:16:57 -05:00
}
if sf.json {
b, err := json.Marshal(&jsonmessage.JSONMessage{
2013-11-28 15:16:57 -05:00
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
}
// StdoutFormatter is a streamFormatter that writes to the standard output.
type StdoutFormatter struct {
io.Writer
*StreamFormatter
}
func (sf *StdoutFormatter) 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
}
// StderrFormatter is a streamFormatter that writes to the standard error.
type StderrFormatter struct {
io.Writer
*StreamFormatter
}
func (sf *StderrFormatter) 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
}