summaryrefslogtreecommitdiff
path: root/cmd/podman/parse
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/podman/parse')
-rw-r--r--cmd/podman/parse/template.go22
-rw-r--r--cmd/podman/parse/template_test.go30
2 files changed, 52 insertions, 0 deletions
diff --git a/cmd/podman/parse/template.go b/cmd/podman/parse/template.go
new file mode 100644
index 000000000..0b80f1b3a
--- /dev/null
+++ b/cmd/podman/parse/template.go
@@ -0,0 +1,22 @@
+package parse
+
+import (
+ "regexp"
+ "strings"
+)
+
+var rangeRegex = regexp.MustCompile(`{{\s*range\s*\.\s*}}.*{{\s*end\s*}}`)
+
+// TODO move to github.com/containers/common/pkg/report
+// EnforceRange ensures that the format string contains a range
+func EnforceRange(format string) string {
+ if !rangeRegex.MatchString(format) {
+ return "{{range .}}" + format + "{{end}}"
+ }
+ return format
+}
+
+// EnforceRange ensures that the format string contains a range
+func HasTable(format string) bool {
+ return strings.HasPrefix(format, "table ")
+}
diff --git a/cmd/podman/parse/template_test.go b/cmd/podman/parse/template_test.go
new file mode 100644
index 000000000..7880d9bec
--- /dev/null
+++ b/cmd/podman/parse/template_test.go
@@ -0,0 +1,30 @@
+package parse
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestEnforceRange(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ {"{{range .}}{{.ID}}{{end}}", "{{range .}}{{.ID}}{{end}}"},
+ {"{{.ID}}", "{{range .}}{{.ID}}{{end}}"},
+ {"{{ range . }}{{ .ID }}{{ end }}", "{{ range . }}{{ .ID }}{{ end }}"},
+ // EnforceRange does not verify syntax or semantics, that will happen later
+ {"{{range .}}{{.ID}}", "{{range .}}{{range .}}{{.ID}}{{end}}"},
+ {".ID", "{{range .}}.ID{{end}}"},
+ }
+
+ for _, tc := range tests {
+ tc := tc
+ label := "TestEnforceRange_" + tc.input
+ t.Run(label, func(t *testing.T) {
+ t.Parallel()
+ assert.Equal(t, tc.expected, EnforceRange(tc.input))
+ })
+ }
+}