From 152c8409709b0b1a54cec1c29db3000be199b668 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 8 Nov 2022 14:12:51 +0100 Subject: [PATCH] pkg/report: fix IsJSON() When a user request --format `{{json .}}` they would want the go template parser to handle it. Currently we overwrite this and assume that `{{json .}}` equals `json`. This is not correct. When the output is a range (array), i.e. podman ps, it should return one json object per line and not a json array which is the case with `json`. This is required for docker compat. Fixes containers/podman#16436 Signed-off-by: Paul Holzinger --- pkg/report/validate.go | 6 ++---- pkg/report/validate_test.go | 13 ++++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/report/validate.go b/pkg/report/validate.go index 987b13a6a..f4e059b09 100644 --- a/pkg/report/validate.go +++ b/pkg/report/validate.go @@ -1,8 +1,6 @@ package report -import "regexp" - -var jsonRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*(\.)?\s*}})\s*$`) +import "strings" // JSONFormat test CLI --format string to be a JSON request // @@ -10,5 +8,5 @@ var jsonRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*(\.)?\s*}})\s*$`) // ... process JSON and output // } func IsJSON(s string) bool { - return jsonRegex.MatchString(s) + return strings.TrimSpace(s) == "json" } diff --git a/pkg/report/validate_test.go b/pkg/report/validate_test.go index 590735e84..3e21e4550 100644 --- a/pkg/report/validate_test.go +++ b/pkg/report/validate_test.go @@ -17,17 +17,16 @@ func TestIsJSON(t *testing.T) { {" json", true}, {" json ", true}, {" json ", true}, - {"{{json}}", true}, - {"{{json }}", true}, - {"{{json .}}", true}, - {"{{ json .}}", true}, - {"{{ json . }}", true}, - {" {{ json . }} ", true}, + {"{{json}}", false}, + {"{{json }}", false}, + {"{{json .}}", false}, + {"{{ json .}}", false}, + {"{{ json . }}", false}, + {" {{ json . }} ", false}, {"{{ json .", false}, {"json . }}", false}, {"{{.ID }} json .", false}, {"json .", false}, - {"{{json.}}", true}, } for _, tc := range tests {