Skip to content

Commit

Permalink
pkg/report: fix IsJSON() #2
Browse files Browse the repository at this point in the history
The PR containers#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 152c840

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Nov 8, 2022
1 parent bf9e18b commit ed6b992
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pkg/report/validate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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
//
// if report.IsJSON(cmd.Flag("format").Value.String()) {
// ... process JSON and output
// }
func IsJSON(s string) bool {
return strings.TrimSpace(s) == "json"
return jsonRegex.MatchString(s)
}
9 changes: 6 additions & 3 deletions pkg/report/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit ed6b992

Please sign in to comment.