From 3f47eba6d5573b1cb28782ccc63fbd8e85011ad7 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 8 Nov 2022 16:50:01 +0100 Subject: [PATCH] pkg/report: fix IsJSON() #2 The PR #1226 was merged to soon, it breaks podman tests and backwards compat. `{{json}}` is not a valid template but it worked before the same as `json` so we should keep that. Fixes up commit 152c8409709b Signed-off-by: Paul Holzinger --- pkg/report/validate.go | 10 ++++++++-- pkg/report/validate_test.go | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/report/validate.go b/pkg/report/validate.go index f4e059b09..4f0f451d5 100644 --- a/pkg/report/validate.go +++ b/pkg/report/validate.go @@ -1,6 +1,12 @@ package report -import "strings" +import ( + "regexp" +) + +// check for json and {{json }} which is not valid go template, +// {{json .}} is valid and thus not matched +var jsonRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*}})\s*$`) // JSONFormat test CLI --format string to be a JSON request // @@ -8,5 +14,5 @@ import "strings" // ... process JSON and output // } func IsJSON(s string) bool { - return strings.TrimSpace(s) == "json" + return jsonRegex.MatchString(s) } diff --git a/pkg/report/validate_test.go b/pkg/report/validate_test.go index 3e21e4550..242634c82 100644 --- a/pkg/report/validate_test.go +++ b/pkg/report/validate_test.go @@ -17,8 +17,8 @@ func TestIsJSON(t *testing.T) { {" json", true}, {" json ", true}, {" json ", true}, - {"{{json}}", false}, - {"{{json }}", false}, + {"{{json}}", true}, + {"{{json }}", true}, {"{{json .}}", false}, {"{{ json .}}", false}, {"{{ json . }}", false},