Repository: docker/compose-cli
Pull request #823 (2020-10-22 14:25:32)

Return json array for  `ps/ls --format json` commands
Print json arrays for 'ps' or 'ls' commands.
```
$ docker ps --format json
[{"ID":"nginx_app","Image":"nginx","Status":"Running","Command":"","Ports":["52.157.252.16:80->80/tcp"]}]

$ docker volume ls --format json
[]

docker compose ls --format json
[{"Name":"e2eTestCompose1602770221964365386","Status":"Running"},{"Name":"e2eTestCompose1601985767794954638","Status":"Running"},{"Name":"e2eTestCompose1601985581055863340","Status":"Running"}]
```

/test-aci
/test-ecs

ulyssessouza: Note that ACI and ECS tests were skipped because the direction of the slashes are incorrect.

Patch:
diff --git a/formatter/formatter.go b/formatter/formatter.go
index 52cd9d0d..c3e84af7 100644
--- a/formatter/formatter.go
+++ b/formatter/formatter.go
@@ -35,15 +35,11 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func
 	case JSON:
 		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 := ToJSON(obj, "", "")
-				if err != nil {
-					return err
-				}
-				_, _ = fmt.Fprint(outWriter, jsonLine)
+			outJSON, err := ToJSON(toJSON, "", "")
+			if err != nil {
+				return err
 			}
+			_, _ = fmt.Fprint(outWriter, outJSON)
 		default:
 			outJSON, err := ToStandardJSON(toJSON)
 			if err != nil {


Test patch:
diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go
index 6657a015..7b86773f 100644
--- a/formatter/formatter_test.go
+++ b/formatter/formatter_test.go
@@ -57,7 +57,6 @@ func TestPrint(t *testing.T) {
 			_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
 		}
 	}, "NAME", "STATUS"))
-	assert.Equal(t, b.String(), `{"Name":"myName1","Status":"myStatus1"}
-{"Name":"myName2","Status":"myStatus2"}
+	assert.Equal(t, b.String(), `[{"Name":"myName1","Status":"myStatus1"},{"Name":"myName2","Status":"myStatus2"}]
 `)
 }
diff --git a/tests/e2e/testdata/ls-out-json-windows.golden b/tests/e2e/testdata/ls-out-json-windows.golden
index 676b7096..c1635424 100644
--- a/tests/e2e/testdata/ls-out-json-windows.golden
+++ b/tests/e2e/testdata/ls-out-json-windows.golden
@@ -1,1 +1,1 @@
-{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}
+[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"npipe:////./pipe/docker_engine","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
diff --git a/tests/e2e/testdata/ls-out-json.golden b/tests/e2e/testdata/ls-out-json.golden
index 34d53cb0..82882753 100644
--- a/tests/e2e/testdata/ls-out-json.golden
+++ b/tests/e2e/testdata/ls-out-json.golden
@@ -1,1 +1,1 @@
-{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}
+[{"Current":true,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","KubernetesEndpoint":"","Type":"moby","Name":"default","StackOrchestrator":"swarm"}]
diff --git a/tests/e2e/testdata/ps-out-example-json.golden b/tests/e2e/testdata/ps-out-example-json.golden
index 2f7ddef9..90711c02 100644
--- a/tests/e2e/testdata/ps-out-example-json.golden
+++ b/tests/e2e/testdata/ps-out-example-json.golden
@@ -1,2 +1,1 @@
-{"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":[]}]