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}}` is not a valid template but it worked before the same
as `json` so we should keep that.

Fixes up commit 152c840

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Nov 8, 2022
1 parent bf9e18b commit 3f47eba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 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 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
//
// 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)
}
4 changes: 2 additions & 2 deletions pkg/report/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 3f47eba

Please sign in to comment.