Change JSON output to individual lines

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-10-01 05:16:02 +02:00
parent 3e9095a873
commit b8a1e6c888
4 changed files with 35 additions and 42 deletions

View File

@ -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
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
} }
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

View File

@ -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) switch reflect.TypeOf(toJSON).Kind() {
if err != nil { case reflect.Slice:
return err 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)
} }

View File

@ -34,8 +34,12 @@ type testStruct struct {
func TestPrint(t *testing.T) { func TestPrint(t *testing.T) {
testList := []testStruct{ testList := []testStruct{
{ {
Name: "myName", Name: "myName1",
Status: "myStatus", 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(), `[ assert.Equal(t, b.String(), `{"Name":"myName1","Status":"myStatus1"}
{ {"Name":"myName2","Status":"myStatus2"}
"Name": "myName", `)
"Status": "myStatus"
}
]`)
} }

View File

@ -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": []
}
]