2015-07-25 08:35:07 +00:00
|
|
|
// Package streamformatter provides helper functions to format a stream.
|
2015-03-18 02:18:41 +00:00
|
|
|
package streamformatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-04-16 19:22:32 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-03-18 02:18:41 +00:00
|
|
|
)
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// StreamFormatter formats a stream, optionally using JSON.
|
2015-03-18 02:18:41 +00:00
|
|
|
type StreamFormatter struct {
|
|
|
|
json bool
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:18:54 +00:00
|
|
|
// NewStreamFormatter returns a simple StreamFormatter
|
|
|
|
func NewStreamFormatter() *StreamFormatter {
|
|
|
|
return &StreamFormatter{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJSONStreamFormatter returns a StreamFormatter configured to stream json
|
|
|
|
func NewJSONStreamFormatter() *StreamFormatter {
|
|
|
|
return &StreamFormatter{true}
|
2015-03-18 02:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const streamNewline = "\r\n"
|
|
|
|
|
|
|
|
var streamNewlineBytes = []byte(streamNewline)
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// FormatStream formats the specified stream.
|
2015-03-18 02:18:41 +00:00
|
|
|
func (sf *StreamFormatter) FormatStream(str string) []byte {
|
|
|
|
if sf.json {
|
|
|
|
b, err := json.Marshal(&jsonmessage.JSONMessage{Stream: str})
|
|
|
|
if err != nil {
|
|
|
|
return sf.FormatError(err)
|
|
|
|
}
|
|
|
|
return append(b, streamNewlineBytes...)
|
|
|
|
}
|
|
|
|
return []byte(str + "\r")
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// FormatStatus formats the specified objects according to the specified format (and id).
|
2015-03-18 02:18:41 +00: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})
|
|
|
|
if err != nil {
|
|
|
|
return sf.FormatError(err)
|
|
|
|
}
|
|
|
|
return append(b, streamNewlineBytes...)
|
|
|
|
}
|
|
|
|
return []byte(str + streamNewline)
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// FormatError formats the specifed error.
|
2015-03-18 02:18:41 +00:00
|
|
|
func (sf *StreamFormatter) FormatError(err error) []byte {
|
|
|
|
if sf.json {
|
|
|
|
jsonError, ok := err.(*jsonmessage.JSONError)
|
|
|
|
if !ok {
|
|
|
|
jsonError = &jsonmessage.JSONError{Message: err.Error()}
|
|
|
|
}
|
|
|
|
if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
|
|
|
|
return append(b, streamNewlineBytes...)
|
|
|
|
}
|
|
|
|
return []byte("{\"error\":\"format error\"}" + streamNewline)
|
|
|
|
}
|
|
|
|
return []byte("Error: " + err.Error() + streamNewline)
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// FormatProgress formats the progress information for a specified action.
|
2015-03-18 02:18:41 +00:00
|
|
|
func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessage.JSONProgress) []byte {
|
|
|
|
if progress == nil {
|
|
|
|
progress = &jsonmessage.JSONProgress{}
|
|
|
|
}
|
|
|
|
if sf.json {
|
|
|
|
b, err := json.Marshal(&jsonmessage.JSONMessage{
|
|
|
|
Status: action,
|
|
|
|
ProgressMessage: progress.String(),
|
|
|
|
Progress: progress,
|
|
|
|
ID: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-28 12:49:54 +00:00
|
|
|
return append(b, streamNewlineBytes...)
|
2015-03-18 02:18:41 +00:00
|
|
|
}
|
|
|
|
endl := "\r"
|
|
|
|
if progress.String() == "" {
|
|
|
|
endl += "\n"
|
|
|
|
}
|
|
|
|
return []byte(action + " " + progress.String() + endl)
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// StdoutFormatter is a streamFormatter that writes to the standard output.
|
|
|
|
type StdoutFormatter struct {
|
2015-03-18 02:18:41 +00:00
|
|
|
io.Writer
|
|
|
|
*StreamFormatter
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
func (sf *StdoutFormatter) Write(buf []byte) (int, error) {
|
2015-03-18 02:18:41 +00:00
|
|
|
formattedBuf := sf.StreamFormatter.FormatStream(string(buf))
|
|
|
|
n, err := sf.Writer.Write(formattedBuf)
|
|
|
|
if n != len(formattedBuf) {
|
|
|
|
return n, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return len(buf), err
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// StderrFormatter is a streamFormatter that writes to the standard error.
|
|
|
|
type StderrFormatter struct {
|
2015-03-18 02:18:41 +00:00
|
|
|
io.Writer
|
|
|
|
*StreamFormatter
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
func (sf *StderrFormatter) Write(buf []byte) (int, error) {
|
2015-03-18 02:18:41 +00:00
|
|
|
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
|
|
|
|
}
|