From ed6b99203e82ffab1a954725761db79a3c45d518 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}}` and `{{json.}}` are not valid templates but it worked before the same as `json` so we should keep that for compat reasons. Fixes up commit 152c8409709b Signed-off-by: Paul Holzinger --- pkg/report/validate.go | 10 ++++++++-- pkg/report/validate_test.go | 9 ++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/report/validate.go b/pkg/report/validate.go index f4e059b09..8d6cff246 100644 --- a/pkg/report/validate.go +++ b/pkg/report/validate.go @@ -1,6 +1,12 @@ package report -import "strings" +import ( + "regexp" +) + +// Check for json, {{json }} and {{ json. }} which are not valid go template, +// {{json .}} is valid and thus not matched to let the template handle it like docker does. +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..e8505f7e7 100644 --- a/pkg/report/validate_test.go +++ b/pkg/report/validate_test.go @@ -17,9 +17,12 @@ func TestIsJSON(t *testing.T) { {" json", true}, {" json ", true}, {" json ", true}, - {"{{json}}", false}, - {"{{json }}", false}, - {"{{json .}}", false}, + // special case, previous regex allowed this template string but it is not actually a valid template + {"{{json}}", true}, + {"{{json }}", true}, + {"{{json.}}", true}, + {"{{ json. }}", true}, + {"{{ json .}}", false}, {"{{ json . }}", false}, {" {{ json . }} ", false},