From b8a1e6c8885d3aa81fdc57d5edcb3d45787f8e85 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Thu, 1 Oct 2020 05:16:02 +0200 Subject: [PATCH] Change JSON output to individual lines Signed-off-by: Ulysses Souza --- cli/cmd/context/ls.go | 16 ++++--------- formatter/formatter.go | 24 +++++++++++++++---- formatter/formatter_test.go | 19 ++++++++------- tests/e2e/testdata/ps-out-example-json.golden | 18 ++------------ 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/cli/cmd/context/ls.go b/cli/cmd/context/ls.go index 5e9cd21da..47ca3dea9 100644 --- a/cli/cmd/context/ls.go +++ b/cli/cmd/context/ls.go @@ -93,20 +93,12 @@ func runList(cmd *cobra.Command, opts lsOpts) error { 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) { for _, c := range view { contextName := c.Name diff --git a/formatter/formatter.go b/formatter/formatter.go index 46af7a459..6686319fb 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -19,6 +19,7 @@ package formatter import ( "fmt" "io" + "reflect" "strings" "github.com/pkg/errors" @@ -27,16 +28,29 @@ import ( ) // 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) { case PRETTY, "": return PrintPrettySection(outWriter, writerFn, headers...) 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: return errors.Wrapf(errdefs.ErrParsingFailed, "format value %q could not be parsed", format) } diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 21668ae18..6657a0152 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -34,8 +34,12 @@ type testStruct struct { func TestPrint(t *testing.T) { 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) } }, "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() 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) } }, "NAME", "STATUS")) - assert.Equal(t, b.String(), `[ - { - "Name": "myName", - "Status": "myStatus" - } -]`) + assert.Equal(t, b.String(), `{"Name":"myName1","Status":"myStatus1"} +{"Name":"myName2","Status":"myStatus2"} +`) } diff --git a/tests/e2e/testdata/ps-out-example-json.golden b/tests/e2e/testdata/ps-out-example-json.golden index c34e5d21b..2f7ddef9b 100644 --- a/tests/e2e/testdata/ps-out-example-json.golden +++ b/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": [] - } -] \ No newline at end of file +{"ID":"id","Image":"nginx","Status":"","Command":"","Ports":[]} +{"ID":"1234","Image":"alpine","Status":"","Command":"","Ports":[]}