Change ExpandFileSource to work with key=val patterns

Signed-off-by: Chris <chrismarkou92@gmail.com>
This commit is contained in:
Chris
2019-02-13 00:24:20 +02:00
committed by Chris Mark
parent e65b45f969
commit 1382d87d7f
2 changed files with 58 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ package add
import (
"fmt"
"strings"
"sigs.k8s.io/kustomize/pkg/fs"
)
@@ -54,10 +55,33 @@ func (a *flagsAndArgs) Validate(args []string) error {
}
func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error {
result, err := globPatterns(fSys, a.FileSources)
if err != nil {
return err
var results []string
var key string
for _, pattern := range a.FileSources {
var patterns []string
key = ""
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 {
return err
}
if key != "" {
if len(result) != 1 {
msg := fmt.Sprintf("%s pattern should not catch more than one file", pattern)
return fmt.Errorf(msg)
}
fileSource := fmt.Sprintf("%s=%s", key, result[0])
results = append(results, fileSource)
} else {
results = append(results, result...)
}
}
a.FileSources = result
a.FileSources = results
return nil
}

View File

@@ -102,3 +102,33 @@ func TestExpandFileSource(t *testing.T) {
t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources)
}
}
func TestExpandFileSourceWithKey(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.Create("dir/fa1")
fakeFS.Create("dir/reademe")
fa := flagsAndArgs{
FileSources: []string{"foo-key=dir/fa*"},
}
fa.ExpandFileSource(fakeFS)
expected := []string{
"foo-key=dir/fa1",
}
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/reademe")
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)
}
}