Refactor pkg/stremformatter with custom constructors instead of passing a boolean

Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
Antonio Murdaca 2015-05-12 20:18:54 +02:00
parent 88a6b39bd3
commit cc22fd7990
2 changed files with 41 additions and 16 deletions

View file

@ -12,8 +12,14 @@ type StreamFormatter struct {
json bool
}
func NewStreamFormatter(json bool) *StreamFormatter {
return &StreamFormatter{json}
// NewStreamFormatter returns a simple StreamFormatter
func NewStreamFormatter() *StreamFormatter {
return &StreamFormatter{}
}
// NewJSONStreamFormatter returns a StreamFormatter configured to stream json
func NewJSONStreamFormatter() *StreamFormatter {
return &StreamFormatter{true}
}
const streamNewline = "\r\n"
@ -62,7 +68,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
progress = &jsonmessage.JSONProgress{}
}
if sf.json {
b, err := json.Marshal(&jsonmessage.JSONMessage{
Status: action,
ProgressMessage: progress.String(),
@ -81,10 +86,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
return []byte(action + " " + progress.String() + endl)
}
func (sf *StreamFormatter) Json() bool {
return sf.json
}
type StdoutFormater struct {
io.Writer
*StreamFormatter