Skip to content

Commit

Permalink
Merge pull request #1227 from Luap99/format-json2
Browse files Browse the repository at this point in the history
pkg/report: fix IsJSON() #2
  • Loading branch information
openshift-merge-robot authored Nov 9, 2022
2 parents b793402 + ed6b992 commit 1e40f47
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 1e40f47

Please sign in to comment.