Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable FindString to search dotD config files #7323

Merged
merged 5 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions pkg/configfilearg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,46 @@ func (p *Parser) stripInvalidFlags(command string, args []string) ([]string, err

func (p *Parser) FindString(args []string, target string) (string, error) {
configFile, isSet := p.findConfigFileFlag(args)
var lastVal string
if configFile != "" {
bytes, err := readConfigFileData(configFile)

_, err := os.Stat(configFile)
if !isSet && os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", err
}

data := yaml.MapSlice{}
if err := yaml.Unmarshal(bytes, &data); err != nil {
files, err := dotDFiles(configFile)
if err != nil {
return "", err
}
files = append([]string{configFile}, files...)
for _, file := range files {
bytes, err := readConfigFileData(file)
if err != nil {
return "", err
}

for _, i := range data {
k, v := convert.ToString(i.Key), convert.ToString(i.Value)
if k == target {
return v, nil
data := yaml.MapSlice{}
if err := yaml.Unmarshal(bytes, &data); err != nil {
return "", err
}
for _, i := range data {
k, v := convert.ToString(i.Key), convert.ToString(i.Value)
isAppend := strings.HasSuffix(k, "+")
k = strings.TrimSuffix(k, "+")
if k == target {
dereknola marked this conversation as resolved.
Show resolved Hide resolved
if isAppend {
lastVal = lastVal + "," + v
} else {
lastVal = v
}
}
}
}
}

return "", nil
return lastVal, nil
}

func (p *Parser) findConfigFileFlag(args []string) (string, bool) {
Expand Down
49 changes: 45 additions & 4 deletions pkg/configfilearg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func Test_UnitParser_findConfigFileFlag(t *testing.T) {
func Test_UnitParser_Parse(t *testing.T) {
testDataOutput := []string{
"--foo-bar=bar-foo",
"--alice=bob",
"--a-slice=1",
"--a-slice=1.5",
"--a-slice=2",
Expand All @@ -237,6 +238,7 @@ func Test_UnitParser_Parse(t *testing.T) {
"--c-slice=three",
"--d-slice=three",
"--d-slice=four",
"--f-string=beta",
"--e-slice=one",
"--e-slice=two",
}
Expand Down Expand Up @@ -376,20 +378,20 @@ func Test_UnitParser_FindString(t *testing.T) {
want: "",
},
{
name: "A custom config yaml exists, target exists",
name: "A custom config exists, target exists",
fields: fields{
FlagNames: []string{"-c", "--config"},
EnvName: "_TEST_ENV",
DefaultConfig: "./testdata/data.yaml",
},
args: args{
osArgs: []string{"-c", "./testdata/data.yaml"},
target: "foo-bar",
target: "alice",
},
want: "baz",
want: "bob",
},
{
name: "A custom config yaml exists, target does not exist",
name: "A custom config exists, target does not exist",
fields: fields{
FlagNames: []string{"-c", "--config"},
EnvName: "_TEST_ENV",
Expand All @@ -401,6 +403,45 @@ func Test_UnitParser_FindString(t *testing.T) {
},
want: "",
},
{
name: "Multiple custom configs exist, target exists in a secondary config",
fields: fields{
FlagNames: []string{"-c", "--config"},
EnvName: "_TEST_ENV",
DefaultConfig: "./testdata/data.yaml",
},
args: args{
osArgs: []string{"-c", "./testdata/data.yaml"},
target: "f-string",
},
want: "beta",
},
{
name: "Multiple custom configs exist, multiple targets exist in multiple secondary config, replacement",
fields: fields{
FlagNames: []string{"-c", "--config"},
EnvName: "_TEST_ENV",
DefaultConfig: "./testdata/data.yaml",
},
args: args{
osArgs: []string{"-c", "./testdata/data.yaml"},
target: "foo-bar",
},
want: "bar-foo",
},
{
name: "Multiple custom configs exist, multiple targets exist in multiple secondary config, appending",
fields: fields{
FlagNames: []string{"-c", "--config"},
EnvName: "_TEST_ENV",
DefaultConfig: "./testdata/data.yaml",
},
args: args{
osArgs: []string{"-c", "./testdata/data.yaml"},
target: "b-string",
},
want: "one,two",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/configfilearg/testdata/data.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
foo-bar: baz
alice: bob
a-slice:
- 1
- "2"
Expand Down
3 changes: 2 additions & 1 deletion pkg/configfilearg/testdata/data.yaml.d/01-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ c-slice:
- two
d-slice:
- one
- two
- two
f-string: beta