Skip to content

Commit

Permalink
pkg/report: fix IsJSON()
Browse files Browse the repository at this point in the history
When a user request --format `{{json .}}` they would want the go
template parser to handle it. Currently we overwrite this and assume
that `{{json .}}` equals `json`. This is not correct. When the output is
a range (array), i.e. podman ps, it should return one json object per
line and not a json array which is the case with `json`.

This is required for docker compat.

Fixes #16436

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Nov 8, 2022
1 parent 627a7c3 commit 738a12d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
6 changes: 2 additions & 4 deletions pkg/report/validate.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package report

import "regexp"

var jsonRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*(\.)?\s*}})\s*$`)
import "strings"

// 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 jsonRegex.MatchString(s)
return strings.TrimSpace(s) == "json"
}
13 changes: 6 additions & 7 deletions pkg/report/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ func TestIsJSON(t *testing.T) {
{" json", true},
{" json ", true},
{" json ", true},
{"{{json}}", true},
{"{{json }}", true},
{"{{json .}}", true},
{"{{ json .}}", true},
{"{{ json . }}", true},
{" {{ json . }} ", true},
{"{{json}}", false},
{"{{json }}", false},
{"{{json .}}", false},
{"{{ json .}}", false},
{"{{ json . }}", false},
{" {{ json . }} ", false},
{"{{ json .", false},
{"json . }}", false},
{"{{.ID }} json .", false},
{"json .", false},
{"{{json.}}", true},
}

for _, tc := range tests {
Expand Down

0 comments on commit 738a12d

Please sign in to comment.