2015-07-25 04:35:07 -04:00
|
|
|
// Package streamformatter provides helper functions to format a stream.
|
2015-03-17 22:18:41 -04:00
|
|
|
package streamformatter
|
2013-11-28 15:16:57 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2014-03-11 13:40:06 -04:00
|
|
|
"io"
|
2015-04-16 15:22:32 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2013-11-28 15:16:57 -05:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// StreamFormatter formats a stream, optionally using JSON.
|
2013-11-28 15:16:57 -05:00
|
|
|
type StreamFormatter struct {
|
|
|
|
json bool
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:18:54 -04: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}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
2014-02-21 01:42:31 -05:00
|
|
|
const streamNewline = "\r\n"
|
|
|
|
|
|
|
|
var streamNewlineBytes = []byte(streamNewline)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// FormatStream formats the specified stream.
|
2013-12-06 14:55:56 -05:00
|
|
|
func (sf *StreamFormatter) FormatStream(str string) []byte {
|
|
|
|
if sf.json {
|
2015-03-17 22:18:41 -04:00
|
|
|
b, err := json.Marshal(&jsonmessage.JSONMessage{Stream: str})
|
2013-12-06 14:55:56 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// 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 {
|
2015-03-17 22:18:41 -04:00
|
|
|
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)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// FormatError formats the specified error.
|
2013-11-28 15:16:57 -05:00
|
|
|
func (sf *StreamFormatter) FormatError(err error) []byte {
|
|
|
|
if sf.json {
|
2015-03-17 22:18:41 -04:00
|
|
|
jsonError, ok := err.(*jsonmessage.JSONError)
|
2013-11-28 15:16:57 -05:00
|
|
|
if !ok {
|
2015-03-17 22:18:41 -04:00
|
|
|
jsonError = &jsonmessage.JSONError{Message: err.Error()}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2015-03-17 22:18:41 -04:00
|
|
|
if b, err := json.Marshal(&jsonmessage.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-03-17 22:18:41 -04:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// FormatProgress formats the progress information for a specified action.
|
2015-12-21 18:02:44 -05:00
|
|
|
func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessage.JSONProgress, aux interface{}) []byte {
|
2013-11-28 15:16:57 -05:00
|
|
|
if progress == nil {
|
2015-03-17 22:18:41 -04:00
|
|
|
progress = &jsonmessage.JSONProgress{}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
if sf.json {
|
2015-12-21 18:02:44 -05:00
|
|
|
var auxJSON *json.RawMessage
|
|
|
|
if aux != nil {
|
|
|
|
auxJSONBytes, err := json.Marshal(aux)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
auxJSON = new(json.RawMessage)
|
|
|
|
*auxJSON = auxJSONBytes
|
|
|
|
}
|
2015-03-17 22:18:41 -04:00
|
|
|
b, err := json.Marshal(&jsonmessage.JSONMessage{
|
2013-11-28 15:16:57 -05:00
|
|
|
Status: action,
|
|
|
|
ProgressMessage: progress.String(),
|
|
|
|
Progress: progress,
|
|
|
|
ID: id,
|
2015-12-21 18:02:44 -05:00
|
|
|
Aux: auxJSON,
|
2013-11-28 15:16:57 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-28 08:49:54 -04:00
|
|
|
return append(b, streamNewlineBytes...)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
// NewProgressOutput returns a progress.Output object that can be passed to
|
|
|
|
// progress.NewProgressReader.
|
|
|
|
func (sf *StreamFormatter) NewProgressOutput(out io.Writer, newLines bool) progress.Output {
|
|
|
|
return &progressOutput{
|
|
|
|
sf: sf,
|
|
|
|
out: out,
|
|
|
|
newLines: newLines,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type progressOutput struct {
|
|
|
|
sf *StreamFormatter
|
|
|
|
out io.Writer
|
|
|
|
newLines bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteProgress formats progress information from a ProgressReader.
|
|
|
|
func (out *progressOutput) WriteProgress(prog progress.Progress) error {
|
|
|
|
var formatted []byte
|
|
|
|
if prog.Message != "" {
|
|
|
|
formatted = out.sf.FormatStatus(prog.ID, prog.Message)
|
|
|
|
} else {
|
|
|
|
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total}
|
2015-12-21 18:02:44 -05:00
|
|
|
formatted = out.sf.FormatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux)
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
_, err := out.out.Write(formatted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.newLines && prog.LastUpdate {
|
|
|
|
_, err = out.out.Write(out.sf.FormatStatus("", ""))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// StdoutFormatter is a streamFormatter that writes to the standard output.
|
|
|
|
type StdoutFormatter struct {
|
2014-03-11 13:40:06 -04:00
|
|
|
io.Writer
|
|
|
|
*StreamFormatter
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
func (sf *StdoutFormatter) Write(buf []byte) (int, error) {
|
2014-03-11 13:40:06 -04: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 04:35:07 -04:00
|
|
|
// StderrFormatter is a streamFormatter that writes to the standard error.
|
|
|
|
type StderrFormatter struct {
|
2014-03-11 13:40:06 -04:00
|
|
|
io.Writer
|
|
|
|
*StreamFormatter
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
func (sf *StderrFormatter) Write(buf []byte) (int, error) {
|
2014-03-11 13:40:06 -04: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
|
|
|
|
}
|