mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #786 from ChrsMark/master
Change ExpandFileSource to work with key=val patterns
This commit is contained in:
@@ -18,6 +18,7 @@ package add
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
@@ -53,11 +54,54 @@ func (a *flagsAndArgs) Validate(args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExpandFileSource normalizes a string list, possibly
|
||||||
|
// containing globs, into a validated, globless list.
|
||||||
|
// For example, this list:
|
||||||
|
// some/path
|
||||||
|
// some/dir/a*
|
||||||
|
// bfile=some/dir/b*
|
||||||
|
// becomes:
|
||||||
|
// some/path
|
||||||
|
// some/dir/airplane
|
||||||
|
// some/dir/ant
|
||||||
|
// some/dir/apple
|
||||||
|
// bfile=some/dir/banana
|
||||||
|
// i.e. everything is converted to a key=value pair,
|
||||||
|
// where the value is always a relative file path,
|
||||||
|
// and the key, if missing, is the same as the value.
|
||||||
|
// In the case where the key is explicitly declared,
|
||||||
|
// the globbing, if present, must have exactly one match.
|
||||||
func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error {
|
func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error {
|
||||||
result, err := globPatterns(fSys, a.FileSources)
|
var results []string
|
||||||
|
for _, pattern := range a.FileSources {
|
||||||
|
var patterns []string
|
||||||
|
key := ""
|
||||||
|
// check if the pattern is in `--from-file=[key=]source` format
|
||||||
|
// and if so split it to send only the file-pattern to glob function
|
||||||
|
s := strings.Split(pattern, "=")
|
||||||
|
if len(s) == 2 {
|
||||||
|
patterns = append(patterns, s[1])
|
||||||
|
key = s[0]
|
||||||
|
} else {
|
||||||
|
patterns = append(patterns, s[0])
|
||||||
|
}
|
||||||
|
result, err := globPatterns(fSys, patterns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
a.FileSources = result
|
// if the format is `--from-file=[key=]source` accept only one result
|
||||||
|
// and extend it with the `key=` prefix
|
||||||
|
if key != "" {
|
||||||
|
if len(result) != 1 {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"'pattern '%s' catches files %v, should catch only one.", pattern, result)
|
||||||
|
}
|
||||||
|
fileSource := fmt.Sprintf("%s=%s", key, result[0])
|
||||||
|
results = append(results, fileSource)
|
||||||
|
} else {
|
||||||
|
results = append(results, result...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.FileSources = results
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ func TestExpandFileSource(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.Create("dir/fa1")
|
fakeFS.Create("dir/fa1")
|
||||||
fakeFS.Create("dir/fa2")
|
fakeFS.Create("dir/fa2")
|
||||||
fakeFS.Create("dir/reademe")
|
fakeFS.Create("dir/readme")
|
||||||
fa := flagsAndArgs{
|
fa := flagsAndArgs{
|
||||||
FileSources: []string{"dir/fa*"},
|
FileSources: []string{"dir/fa*"},
|
||||||
}
|
}
|
||||||
@@ -102,3 +102,37 @@ func TestExpandFileSource(t *testing.T) {
|
|||||||
t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources)
|
t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExpandFileSourceWithKey(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.Create("dir/faaaaaaaaaabbbbbbbbbccccccccccccccccc")
|
||||||
|
fakeFS.Create("dir/foobar")
|
||||||
|
fakeFS.Create("dir/simplebar")
|
||||||
|
fakeFS.Create("dir/readme")
|
||||||
|
fa := flagsAndArgs{
|
||||||
|
FileSources: []string{"foo-key=dir/fa*", "bar-key=dir/foobar", "dir/simplebar"},
|
||||||
|
}
|
||||||
|
fa.ExpandFileSource(fakeFS)
|
||||||
|
expected := []string{
|
||||||
|
"foo-key=dir/faaaaaaaaaabbbbbbbbbccccccccccccccccc",
|
||||||
|
"bar-key=dir/foobar",
|
||||||
|
"dir/simplebar",
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(fa.FileSources, expected) {
|
||||||
|
t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpandFileSourceWithKeyAndError(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.Create("dir/fa1")
|
||||||
|
fakeFS.Create("dir/fa2")
|
||||||
|
fakeFS.Create("dir/readme")
|
||||||
|
fa := flagsAndArgs{
|
||||||
|
FileSources: []string{"foo-key=dir/fa*"},
|
||||||
|
}
|
||||||
|
err := fa.ExpandFileSource(fakeFS)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("FileSources should not be correctly expanded: %v", fa.FileSources)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user