Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 83716a5

Browse files
aiordacheUlysses Souza
authored andcommitted
Return json array for ps/ls --format json commands
Signed-off-by: aiordache <[email protected]>
1 parent b95c9a9 commit 83716a5

File tree

5 files changed

+8
-14
lines changed

5 files changed

+8
-14
lines changed

formatter/formatter.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func
3535
case JSON:
3636
switch reflect.TypeOf(toJSON).Kind() {
3737
case reflect.Slice:
38-
s := reflect.ValueOf(toJSON)
39-
for i := 0; i < s.Len(); i++ {
40-
obj := s.Index(i).Interface()
41-
jsonLine, err := ToJSON(obj, "", "")
42-
if err != nil {
43-
return err
44-
}
45-
_, _ = fmt.Fprint(outWriter, jsonLine)
38+
outJSON, err := ToJSON(toJSON, "", "")
39+
if err != nil {
40+
return err
4641
}
42+
_, _ = fmt.Fprint(outWriter, outJSON)
4743
default:
4844
outJSON, err := ToStandardJSON(toJSON)
4945
if err != nil {

formatter/formatter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func TestPrint(t *testing.T) {
5757
_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
5858
}
5959
}, "NAME", "STATUS"))
60-
assert.Equal(t, b.String(), `{"Name":"myName1","Status":"myStatus1"}
61-
{"Name":"myName2","Status":"myStatus2"}
60+
assert.Equal(t, b.String(), `[{"Name":"myName1","Status":"myStatus1"},{"Name":"myName2","Status":"myStatus2"}]
6261
`)
6362
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}
1+
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}
1+
[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
{"ID":"id","Image":"nginx","Status":"","Command":"","Ports":[]}
2-
{"ID":"1234","Image":"alpine","Status":"","Command":"","Ports":[]}
1+
[{"ID":"id","Image":"nginx","Status":"","Command":"","Ports":[]},{"ID":"1234","Image":"alpine","Status":"","Command":"","Ports":[]}]

0 commit comments

Comments
 (0)