Browse Source

Change JSON output to individual lines

Signed-off-by: Ulysses Souza <[email protected]>
Ulysses Souza 5 years ago
parent
commit
b8a1e6c888

+ 4 - 12
cli/cmd/context/ls.go

@@ -93,20 +93,12 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
 		return nil
 		return nil
 	}
 	}
 
 
-	view := viewFromContextList(contexts, currentContext)
-
-	if opts.json || opts.format == formatter.JSON {
-		for _, l := range view {
-			outJSON, err := formatter.ToCompressedJSON(l)
-			if err != nil {
-				return err
-			}
-			_, _ = fmt.Fprintln(os.Stdout, outJSON)
-		}
-		return nil
+	if opts.json {
+		opts.format = formatter.JSON
 	}
 	}
 
 
-	return formatter.Print(view, formatter.PRETTY, os.Stdout,
+	view := viewFromContextList(contexts, currentContext)
+	return formatter.Print(view, opts.format, os.Stdout,
 		func(w io.Writer) {
 		func(w io.Writer) {
 			for _, c := range view {
 			for _, c := range view {
 				contextName := c.Name
 				contextName := c.Name

+ 19 - 5
formatter/formatter.go

@@ -19,6 +19,7 @@ package formatter
 import (
 import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
+	"reflect"
 	"strings"
 	"strings"
 
 
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
@@ -27,16 +28,29 @@ import (
 )
 )
 
 
 // Print prints formatted lists in different formats
 // Print prints formatted lists in different formats
-func Print(list interface{}, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error {
+func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error {
 	switch strings.ToLower(format) {
 	switch strings.ToLower(format) {
 	case PRETTY, "":
 	case PRETTY, "":
 		return PrintPrettySection(outWriter, writerFn, headers...)
 		return PrintPrettySection(outWriter, writerFn, headers...)
 	case JSON:
 	case JSON:
-		outJSON, err := ToStandardJSON(list)
-		if err != nil {
-			return err
+		switch reflect.TypeOf(toJSON).Kind() {
+		case reflect.Slice:
+			s := reflect.ValueOf(toJSON)
+			for i := 0; i < s.Len(); i++ {
+				obj := s.Index(i).Interface()
+				jsonLine, err := ToCompressedJSON(obj)
+				if err != nil {
+					return err
+				}
+				_, _ = fmt.Fprintln(outWriter, jsonLine)
+			}
+		default:
+			outJSON, err := ToStandardJSON(toJSON)
+			if err != nil {
+				return err
+			}
+			_, _ = fmt.Fprintln(outWriter, outJSON)
 		}
 		}
-		_, _ = fmt.Fprint(outWriter, outJSON)
 	default:
 	default:
 		return errors.Wrapf(errdefs.ErrParsingFailed, "format value %q could not be parsed", format)
 		return errors.Wrapf(errdefs.ErrParsingFailed, "format value %q could not be parsed", format)
 	}
 	}

+ 10 - 9
formatter/formatter_test.go

@@ -34,8 +34,12 @@ type testStruct struct {
 func TestPrint(t *testing.T) {
 func TestPrint(t *testing.T) {
 	testList := []testStruct{
 	testList := []testStruct{
 		{
 		{
-			Name:   "myName",
-			Status: "myStatus",
+			Name:   "myName1",
+			Status: "myStatus1",
+		},
+		{
+			Name:   "myName2",
+			Status: "myStatus2",
 		},
 		},
 	}
 	}
 
 
@@ -45,7 +49,7 @@ func TestPrint(t *testing.T) {
 			_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
 			_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
 		}
 		}
 	}, "NAME", "STATUS"))
 	}, "NAME", "STATUS"))
-	assert.Equal(t, b.String(), "NAME                STATUS\nmyName              myStatus\n")
+	assert.Equal(t, b.String(), "NAME                STATUS\nmyName1             myStatus1\nmyName2             myStatus2\n")
 
 
 	b.Reset()
 	b.Reset()
 	assert.NilError(t, Print(testList, JSON, b, func(w io.Writer) {
 	assert.NilError(t, Print(testList, JSON, b, func(w io.Writer) {
@@ -53,10 +57,7 @@ func TestPrint(t *testing.T) {
 			_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
 			_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
 		}
 		}
 	}, "NAME", "STATUS"))
 	}, "NAME", "STATUS"))
-	assert.Equal(t, b.String(), `[
-    {
-        "Name": "myName",
-        "Status": "myStatus"
-    }
-]`)
+	assert.Equal(t, b.String(), `{"Name":"myName1","Status":"myStatus1"}
+{"Name":"myName2","Status":"myStatus2"}
+`)
 }
 }

+ 2 - 16
tests/e2e/testdata/ps-out-example-json.golden

@@ -1,16 +1,2 @@
-[
-    {
-        "ID": "id",
-        "Image": "nginx",
-        "Status": "",
-        "Command": "",
-        "Ports": []
-    },
-    {
-        "ID": "1234",
-        "Image": "alpine",
-        "Status": "",
-        "Command": "",
-        "Ports": []
-    }
-]
+{"ID":"id","Image":"nginx","Status":"","Command":"","Ports":[]}
+{"ID":"1234","Image":"alpine","Status":"","Command":"","Ports":[]}