Merge pull request #3732 from tonistiigi/imagetools-manifest-regex
imagetools: use regex for manifest template matching
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -162,7 +163,7 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isWholeManifestTemplate(format string) bool {
|
func isWholeManifestTemplate(format string) bool {
|
||||||
return strings.TrimSpace(format) == "{{.Manifest}}"
|
return regexp.MustCompile(`^\{\{\s*\.Manifest\s*\}\}$`).MatchString(strings.TrimSpace(format))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Printer) printManifestList(out io.Writer) error {
|
func (p *Printer) printManifestList(out io.Writer) error {
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package imagetools
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestIsWholeManifestTemplate(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
format string
|
||||||
|
match bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "exact",
|
||||||
|
format: "{{.Manifest}}",
|
||||||
|
match: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "trimmed-whitespace",
|
||||||
|
format: " {{.Manifest}} ",
|
||||||
|
match: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "inner-whitespace",
|
||||||
|
format: "{{ .Manifest }}",
|
||||||
|
match: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not-whole-template",
|
||||||
|
format: "{{.Manifest.Digest}}",
|
||||||
|
match: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "split-field-name-does-not-match",
|
||||||
|
format: "{{.Mani fest }}",
|
||||||
|
match: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "extra-text",
|
||||||
|
format: "{{.Manifest}}\n",
|
||||||
|
match: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if got := isWholeManifestTemplate(tc.format); got != tc.match {
|
||||||
|
t.Fatalf("isWholeManifestTemplate(%q) = %v, want %v", tc.format, got, tc.match)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user