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
|
|
|
)
|
|
|
|
|
2014-02-21 01:42:31 -05:00
|
|
|
const streamNewline = "\r\n"
|
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
type jsonProgressFormatter struct{}
|
2014-02-21 01:42:31 -05:00
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
func appendNewline(source []byte) []byte {
|
|
|
|
return append(source, []byte(streamNewline)...)
|
2013-12-06 14:55:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// FormatStatus formats the specified objects according to the specified format (and id).
|
2017-05-01 14:54:56 -04:00
|
|
|
func FormatStatus(id, format string, a ...interface{}) []byte {
|
2013-11-28 15:16:57 -05:00
|
|
|
str := fmt.Sprintf(format, a...)
|
2017-05-01 14:54:56 -04:00
|
|
|
b, err := json.Marshal(&jsonmessage.JSONMessage{ID: id, Status: str})
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2017-05-01 14:54:56 -04:00
|
|
|
return appendNewline(b)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
// FormatError formats the error as a JSON object
|
|
|
|
func FormatError(err error) []byte {
|
|
|
|
jsonError, ok := err.(*jsonmessage.JSONError)
|
|
|
|
if !ok {
|
|
|
|
jsonError = &jsonmessage.JSONError{Message: err.Error()}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2017-05-01 14:54:56 -04:00
|
|
|
if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
|
|
|
|
return appendNewline(b)
|
|
|
|
}
|
|
|
|
return []byte(`{"error":"format error"}` + streamNewline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *jsonProgressFormatter) formatStatus(id, format string, a ...interface{}) []byte {
|
|
|
|
return FormatStatus(id, format, a...)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2015-03-17 22:18:41 -04:00
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
// formatProgress formats the progress information for a specified action.
|
|
|
|
func (sf *jsonProgressFormatter) 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
|
|
|
}
|
2017-05-01 14:54:56 -04:00
|
|
|
var auxJSON *json.RawMessage
|
|
|
|
if aux != nil {
|
|
|
|
auxJSONBytes, err := json.Marshal(aux)
|
2013-11-28 15:16:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-01 14:54:56 -04:00
|
|
|
auxJSON = new(json.RawMessage)
|
|
|
|
*auxJSON = auxJSONBytes
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(&jsonmessage.JSONMessage{
|
|
|
|
Status: action,
|
|
|
|
ProgressMessage: progress.String(),
|
|
|
|
Progress: progress,
|
|
|
|
ID: id,
|
|
|
|
Aux: auxJSON,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return appendNewline(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawProgressFormatter struct{}
|
|
|
|
|
|
|
|
func (sf *rawProgressFormatter) formatStatus(id, format string, a ...interface{}) []byte {
|
|
|
|
return []byte(fmt.Sprintf(format, a...) + streamNewline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *rawProgressFormatter) formatProgress(id, action string, progress *jsonmessage.JSONProgress, aux interface{}) []byte {
|
|
|
|
if progress == nil {
|
|
|
|
progress = &jsonmessage.JSONProgress{}
|
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.
|
2017-05-01 14:54:56 -04:00
|
|
|
func NewProgressOutput(out io.Writer) progress.Output {
|
|
|
|
return &progressOutput{sf: &rawProgressFormatter{}, out: out, newLines: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJSONProgressOutput returns a progress.Output that that formats output
|
|
|
|
// using JSON objects
|
|
|
|
func NewJSONProgressOutput(out io.Writer, newLines bool) progress.Output {
|
|
|
|
return &progressOutput{sf: &jsonProgressFormatter{}, out: out, newLines: newLines}
|
|
|
|
}
|
|
|
|
|
|
|
|
type formatProgress interface {
|
|
|
|
formatStatus(id, format string, a ...interface{}) []byte
|
|
|
|
formatProgress(id, action string, progress *jsonmessage.JSONProgress, aux interface{}) []byte
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type progressOutput struct {
|
2017-05-01 14:54:56 -04:00
|
|
|
sf formatProgress
|
2015-11-13 19:59:01 -05:00
|
|
|
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 != "" {
|
2017-05-01 14:54:56 -04:00
|
|
|
formatted = out.sf.formatStatus(prog.ID, prog.Message)
|
2015-11-13 19:59:01 -05:00
|
|
|
} else {
|
2017-05-03 00:22:56 -04:00
|
|
|
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts, Units: prog.Units}
|
2017-05-01 14:54:56 -04: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 {
|
2017-05-01 14:54:56 -04:00
|
|
|
_, err = out.out.Write(out.sf.formatStatus("", ""))
|
2015-11-13 19:59:01 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-06 08:33:56 -04:00
|
|
|
|
|
|
|
// AuxFormatter is a streamFormatter that writes aux progress messages
|
|
|
|
type AuxFormatter struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit emits the given interface as an aux progress message
|
|
|
|
func (sf *AuxFormatter) Emit(aux interface{}) error {
|
|
|
|
auxJSONBytes, err := json.Marshal(aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
auxJSON := new(json.RawMessage)
|
|
|
|
*auxJSON = auxJSONBytes
|
|
|
|
msgJSON, err := json.Marshal(&jsonmessage.JSONMessage{Aux: auxJSON})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msgJSON = appendNewline(msgJSON)
|
|
|
|
n, err := sf.Writer.Write(msgJSON)
|
|
|
|
if n != len(msgJSON) {
|
|
|
|
return io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|