Fixes hacks from progressreader refactor
related to #10959 Signed-off-by: bobby abbott <ttobbaybbob@gmail.com>
This commit is contained in:
parent
0ee2488659
commit
98e8ec854e
5 changed files with 396 additions and 26 deletions
113
streamformatter/streamformatter.go
Normal file
113
streamformatter/streamformatter.go
Normal file
|
@ -0,0 +1,113 @@
|
|||
package streamformatter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/docker/docker/pkg/jsonmessage"
|
||||
"io"
|
||||
)
|
||||
|
||||
type StreamFormatter struct {
|
||||
json bool
|
||||
}
|
||||
|
||||
func NewStreamFormatter(json bool) *StreamFormatter {
|
||||
return &StreamFormatter{json}
|
||||
}
|
||||
|
||||
const streamNewline = "\r\n"
|
||||
|
||||
var streamNewlineBytes = []byte(streamNewline)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
return b
|
||||
}
|
||||
endl := "\r"
|
||||
if progress.String() == "" {
|
||||
endl += "\n"
|
||||
}
|
||||
return []byte(action + " " + progress.String() + endl)
|
||||
}
|
||||
|
||||
func (sf *StreamFormatter) Json() bool {
|
||||
return sf.json
|
||||
}
|
||||
|
||||
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
|
||||
}
|
68
streamformatter/streamformatter_test.go
Normal file
68
streamformatter/streamformatter_test.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package streamformatter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/docker/docker/pkg/jsonmessage"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatStream(t *testing.T) {
|
||||
sf := NewStreamFormatter(true)
|
||||
res := sf.FormatStream("stream")
|
||||
if string(res) != `{"stream":"stream"}`+"\r\n" {
|
||||
t.Fatalf("%q", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatStatus(t *testing.T) {
|
||||
sf := NewStreamFormatter(true)
|
||||
res := sf.FormatStatus("ID", "%s%d", "a", 1)
|
||||
if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" {
|
||||
t.Fatalf("%q", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatSimpleError(t *testing.T) {
|
||||
sf := NewStreamFormatter(true)
|
||||
res := sf.FormatError(errors.New("Error for formatter"))
|
||||
if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" {
|
||||
t.Fatalf("%q", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatJSONError(t *testing.T) {
|
||||
sf := NewStreamFormatter(true)
|
||||
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
|
||||
res := sf.FormatError(err)
|
||||
if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" {
|
||||
t.Fatalf("%q", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatProgress(t *testing.T) {
|
||||
sf := NewStreamFormatter(true)
|
||||
progress := &jsonmessage.JSONProgress{
|
||||
Current: 15,
|
||||
Total: 30,
|
||||
Start: 1,
|
||||
}
|
||||
res := sf.FormatProgress("id", "action", progress)
|
||||
msg := &jsonmessage.JSONMessage{}
|
||||
if err := json.Unmarshal(res, msg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if msg.ID != "id" {
|
||||
t.Fatalf("ID must be 'id', got: %s", msg.ID)
|
||||
}
|
||||
if msg.Status != "action" {
|
||||
t.Fatalf("Status must be 'action', got: %s", msg.Status)
|
||||
}
|
||||
if msg.ProgressMessage != progress.String() {
|
||||
t.Fatalf("ProgressMessage must be %s, got: %s", progress.String(), msg.ProgressMessage)
|
||||
}
|
||||
if !reflect.DeepEqual(msg.Progress, progress) {
|
||||
t.Fatal("Original progress not equals progress from FormatProgress")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue