Browse code

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

Signed-off-by: Antonio Murdaca <me@runcom.ninja>

Antonio Murdaca authored on 2015/05/13 03:18:54
Showing 8 changed files
... ...
@@ -199,7 +199,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
199 199
 	// Setup an upload progress bar
200 200
 	// FIXME: ProgressReader shouldn't be this annoying to use
201 201
 	if context != nil {
202
-		sf := streamformatter.NewStreamFormatter(false)
202
+		sf := streamformatter.NewStreamFormatter()
203 203
 		body = progressreader.New(progressreader.Config{
204 204
 			In:        context,
205 205
 			Out:       cli.out,
... ...
@@ -730,7 +730,7 @@ func (s *Server) postImagesCreate(version version.Version, w http.ResponseWriter
730 730
 		if !output.Flushed() {
731 731
 			return err
732 732
 		}
733
-		sf := streamformatter.NewStreamFormatter(true)
733
+		sf := streamformatter.NewJSONStreamFormatter()
734 734
 		output.Write(sf.FormatError(err))
735 735
 	}
736 736
 
... ...
@@ -814,7 +814,7 @@ func (s *Server) postImagesPush(version version.Version, w http.ResponseWriter,
814 814
 		if !output.Flushed() {
815 815
 			return err
816 816
 		}
817
-		sf := streamformatter.NewStreamFormatter(true)
817
+		sf := streamformatter.NewJSONStreamFormatter()
818 818
 		output.Write(sf.FormatError(err))
819 819
 	}
820 820
 	return nil
... ...
@@ -843,7 +843,7 @@ func (s *Server) getImagesGet(version version.Version, w http.ResponseWriter, r
843 843
 		if !output.Flushed() {
844 844
 			return err
845 845
 		}
846
-		sf := streamformatter.NewStreamFormatter(true)
846
+		sf := streamformatter.NewJSONStreamFormatter()
847 847
 		output.Write(sf.FormatError(err))
848 848
 	}
849 849
 	return nil
... ...
@@ -1234,7 +1234,7 @@ func (s *Server) postBuild(version version.Version, w http.ResponseWriter, r *ht
1234 1234
 		if !output.Flushed() {
1235 1235
 			return err
1236 1236
 		}
1237
-		sf := streamformatter.NewStreamFormatter(true)
1237
+		sf := streamformatter.NewJSONStreamFormatter()
1238 1238
 		w.Write(sf.FormatError(err))
1239 1239
 	}
1240 1240
 	return nil
... ...
@@ -141,7 +141,7 @@ func Build(d *daemon.Daemon, buildConfig *Config) error {
141 141
 	}
142 142
 	defer context.Close()
143 143
 
144
-	sf := streamformatter.NewStreamFormatter(true)
144
+	sf := streamformatter.NewJSONStreamFormatter()
145 145
 
146 146
 	builder := &Builder{
147 147
 		Daemon: d,
... ...
@@ -22,7 +22,7 @@ type ImageImportConfig struct {
22 22
 
23 23
 func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig *ImageImportConfig) error {
24 24
 	var (
25
-		sf      = streamformatter.NewStreamFormatter(true)
25
+		sf      = streamformatter.NewJSONStreamFormatter()
26 26
 		archive archive.ArchiveReader
27 27
 		resp    *http.Response
28 28
 	)
... ...
@@ -29,7 +29,7 @@ type ImagePullConfig struct {
29 29
 
30 30
 func (s *TagStore) Pull(image string, tag string, imagePullConfig *ImagePullConfig) error {
31 31
 	var (
32
-		sf = streamformatter.NewStreamFormatter(true)
32
+		sf = streamformatter.NewJSONStreamFormatter()
33 33
 	)
34 34
 
35 35
 	// Resolve the Repository name from fqn to RepositoryInfo
... ...
@@ -495,7 +495,7 @@ func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint *
495 495
 // FIXME: Allow to interrupt current push when new push of same image is done.
496 496
 func (s *TagStore) Push(localName string, imagePushConfig *ImagePushConfig) error {
497 497
 	var (
498
-		sf = streamformatter.NewStreamFormatter(true)
498
+		sf = streamformatter.NewJSONStreamFormatter()
499 499
 	)
500 500
 
501 501
 	// Resolve the Repository name from fqn to RepositoryInfo
... ...
@@ -12,8 +12,14 @@ type StreamFormatter struct {
12 12
 	json bool
13 13
 }
14 14
 
15
-func NewStreamFormatter(json bool) *StreamFormatter {
16
-	return &StreamFormatter{json}
15
+// NewStreamFormatter returns a simple StreamFormatter
16
+func NewStreamFormatter() *StreamFormatter {
17
+	return &StreamFormatter{}
18
+}
19
+
20
+// NewJSONStreamFormatter returns a StreamFormatter configured to stream json
21
+func NewJSONStreamFormatter() *StreamFormatter {
22
+	return &StreamFormatter{true}
17 23
 }
18 24
 
19 25
 const streamNewline = "\r\n"
... ...
@@ -62,7 +68,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
62 62
 		progress = &jsonmessage.JSONProgress{}
63 63
 	}
64 64
 	if sf.json {
65
-
66 65
 		b, err := json.Marshal(&jsonmessage.JSONMessage{
67 66
 			Status:          action,
68 67
 			ProgressMessage: progress.String(),
... ...
@@ -81,10 +86,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
81 81
 	return []byte(action + " " + progress.String() + endl)
82 82
 }
83 83
 
84
-func (sf *StreamFormatter) Json() bool {
85
-	return sf.json
86
-}
87
-
88 84
 type StdoutFormater struct {
89 85
 	io.Writer
90 86
 	*StreamFormatter
... ...
@@ -10,31 +10,55 @@ import (
10 10
 )
11 11
 
12 12
 func TestFormatStream(t *testing.T) {
13
-	sf := NewStreamFormatter(true)
13
+	sf := NewStreamFormatter()
14
+	res := sf.FormatStream("stream")
15
+	if string(res) != "stream"+"\r" {
16
+		t.Fatalf("%q", res)
17
+	}
18
+}
19
+
20
+func TestFormatJSONStatus(t *testing.T) {
21
+	sf := NewStreamFormatter()
22
+	res := sf.FormatStatus("ID", "%s%d", "a", 1)
23
+	if string(res) != "a1\r\n" {
24
+		t.Fatalf("%q", res)
25
+	}
26
+}
27
+
28
+func TestFormatSimpleError(t *testing.T) {
29
+	sf := NewStreamFormatter()
30
+	res := sf.FormatError(errors.New("Error for formatter"))
31
+	if string(res) != "Error: Error for formatter\r\n" {
32
+		t.Fatalf("%q", res)
33
+	}
34
+}
35
+
36
+func TestJSONFormatStream(t *testing.T) {
37
+	sf := NewJSONStreamFormatter()
14 38
 	res := sf.FormatStream("stream")
15 39
 	if string(res) != `{"stream":"stream"}`+"\r\n" {
16 40
 		t.Fatalf("%q", res)
17 41
 	}
18 42
 }
19 43
 
20
-func TestFormatStatus(t *testing.T) {
21
-	sf := NewStreamFormatter(true)
44
+func TestJSONFormatStatus(t *testing.T) {
45
+	sf := NewJSONStreamFormatter()
22 46
 	res := sf.FormatStatus("ID", "%s%d", "a", 1)
23 47
 	if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" {
24 48
 		t.Fatalf("%q", res)
25 49
 	}
26 50
 }
27 51
 
28
-func TestFormatSimpleError(t *testing.T) {
29
-	sf := NewStreamFormatter(true)
52
+func TestJSONFormatSimpleError(t *testing.T) {
53
+	sf := NewJSONStreamFormatter()
30 54
 	res := sf.FormatError(errors.New("Error for formatter"))
31 55
 	if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" {
32 56
 		t.Fatalf("%q", res)
33 57
 	}
34 58
 }
35 59
 
36
-func TestFormatJSONError(t *testing.T) {
37
-	sf := NewStreamFormatter(true)
60
+func TestJSONFormatJSONError(t *testing.T) {
61
+	sf := NewJSONStreamFormatter()
38 62
 	err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
39 63
 	res := sf.FormatError(err)
40 64
 	if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" {
... ...
@@ -42,8 +66,8 @@ func TestFormatJSONError(t *testing.T) {
42 42
 	}
43 43
 }
44 44
 
45
-func TestFormatProgress(t *testing.T) {
46
-	sf := NewStreamFormatter(true)
45
+func TestJSONFormatProgress(t *testing.T) {
46
+	sf := NewJSONStreamFormatter()
47 47
 	progress := &jsonmessage.JSONProgress{
48 48
 		Current: 15,
49 49
 		Total:   30,