github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/pkg/streamformatter/streamformatter.go (about)

     1  package streamformatter
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/docker/docker/pkg/jsonmessage"
     9  )
    10  
    11  type StreamFormatter struct {
    12  	json bool
    13  }
    14  
    15  func NewStreamFormatter(json bool) *StreamFormatter {
    16  	return &StreamFormatter{json}
    17  }
    18  
    19  const streamNewline = "\r\n"
    20  
    21  var streamNewlineBytes = []byte(streamNewline)
    22  
    23  func (sf *StreamFormatter) FormatStream(str string) []byte {
    24  	if sf.json {
    25  		b, err := json.Marshal(&jsonmessage.JSONMessage{Stream: str})
    26  		if err != nil {
    27  			return sf.FormatError(err)
    28  		}
    29  		return append(b, streamNewlineBytes...)
    30  	}
    31  	return []byte(str + "\r")
    32  }
    33  
    34  func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte {
    35  	str := fmt.Sprintf(format, a...)
    36  	if sf.json {
    37  		b, err := json.Marshal(&jsonmessage.JSONMessage{ID: id, Status: str})
    38  		if err != nil {
    39  			return sf.FormatError(err)
    40  		}
    41  		return append(b, streamNewlineBytes...)
    42  	}
    43  	return []byte(str + streamNewline)
    44  }
    45  
    46  func (sf *StreamFormatter) FormatError(err error) []byte {
    47  	if sf.json {
    48  		jsonError, ok := err.(*jsonmessage.JSONError)
    49  		if !ok {
    50  			jsonError = &jsonmessage.JSONError{Message: err.Error()}
    51  		}
    52  		if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
    53  			return append(b, streamNewlineBytes...)
    54  		}
    55  		return []byte("{\"error\":\"format error\"}" + streamNewline)
    56  	}
    57  	return []byte("Error: " + err.Error() + streamNewline)
    58  }
    59  
    60  func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessage.JSONProgress) []byte {
    61  	if progress == nil {
    62  		progress = &jsonmessage.JSONProgress{}
    63  	}
    64  	if sf.json {
    65  
    66  		b, err := json.Marshal(&jsonmessage.JSONMessage{
    67  			Status:          action,
    68  			ProgressMessage: progress.String(),
    69  			Progress:        progress,
    70  			ID:              id,
    71  		})
    72  		if err != nil {
    73  			return nil
    74  		}
    75  		return b
    76  	}
    77  	endl := "\r"
    78  	if progress.String() == "" {
    79  		endl += "\n"
    80  	}
    81  	return []byte(action + " " + progress.String() + endl)
    82  }
    83  
    84  func (sf *StreamFormatter) Json() bool {
    85  	return sf.json
    86  }
    87  
    88  type StdoutFormater struct {
    89  	io.Writer
    90  	*StreamFormatter
    91  }
    92  
    93  func (sf *StdoutFormater) Write(buf []byte) (int, error) {
    94  	formattedBuf := sf.StreamFormatter.FormatStream(string(buf))
    95  	n, err := sf.Writer.Write(formattedBuf)
    96  	if n != len(formattedBuf) {
    97  		return n, io.ErrShortWrite
    98  	}
    99  	return len(buf), err
   100  }
   101  
   102  type StderrFormater struct {
   103  	io.Writer
   104  	*StreamFormatter
   105  }
   106  
   107  func (sf *StderrFormater) Write(buf []byte) (int, error) {
   108  	formattedBuf := sf.StreamFormatter.FormatStream("\033[91m" + string(buf) + "\033[0m")
   109  	n, err := sf.Writer.Write(formattedBuf)
   110  	if n != len(formattedBuf) {
   111  		return n, io.ErrShortWrite
   112  	}
   113  	return len(buf), err
   114  }