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

View File

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

View File

@ -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"}
`)
}

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