mirror of https://github.com/docker/compose.git
Change JSON output to individual lines
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
3e9095a873
commit
b8a1e6c888
|
@ -93,20 +93,12 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
if opts.json {
|
||||
opts.format = formatter.JSON
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return formatter.Print(view, formatter.PRETTY, os.Stdout,
|
||||
return formatter.Print(view, opts.format, os.Stdout,
|
||||
func(w io.Writer) {
|
||||
for _, c := range view {
|
||||
contextName := c.Name
|
||||
|
|
|
@ -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)
|
||||
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.Fprint(outWriter, outJSON)
|
||||
_, _ = fmt.Fprintln(outWriter, jsonLine)
|
||||
}
|
||||
default:
|
||||
outJSON, err := ToStandardJSON(toJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = fmt.Fprintln(outWriter, outJSON)
|
||||
}
|
||||
default:
|
||||
return errors.Wrapf(errdefs.ErrParsingFailed, "format value %q could not be parsed", format)
|
||||
}
|
||||
|
|
|
@ -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"}
|
||||
`)
|
||||
}
|
||||
|
|
|
@ -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":[]}
|
||||
|
|
Loading…
Reference in New Issue