From ce1a4474ca6655aec1243d8b5a3817b79c123f46 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Wed, 19 Apr 2023 14:33:35 -0700 Subject: [PATCH 1/5] Enable FindString to search dotD config files Signed-off-by: Derek Nola --- pkg/configfilearg/parser.go | 27 ++++++++++++++++++++------- pkg/configfilearg/parser_test.go | 17 +++++++++++++++-- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/pkg/configfilearg/parser.go b/pkg/configfilearg/parser.go index 764aaf0c6caf..a8784388fec0 100644 --- a/pkg/configfilearg/parser.go +++ b/pkg/configfilearg/parser.go @@ -99,22 +99,35 @@ 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) 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) + if k == target { + return v, nil + } } } } diff --git a/pkg/configfilearg/parser_test.go b/pkg/configfilearg/parser_test.go index 776d43774f17..373e8b691a97 100644 --- a/pkg/configfilearg/parser_test.go +++ b/pkg/configfilearg/parser_test.go @@ -376,7 +376,7 @@ 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", @@ -389,7 +389,7 @@ func Test_UnitParser_FindString(t *testing.T) { want: "baz", }, { - 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", @@ -401,6 +401,19 @@ 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: "b-string", + }, + want: "one", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 5c92fef709a0901ccb62f24958ee2f4f7dc13c16 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Wed, 19 Apr 2023 15:46:47 -0700 Subject: [PATCH 2/5] Address multiple arg cases Signed-off-by: Derek Nola --- pkg/configfilearg/parser.go | 13 +++++--- pkg/configfilearg/parser_test.go | 32 +++++++++++++++++-- pkg/configfilearg/testdata/data.yaml | 2 +- .../testdata/data.yaml.d/01-data.yml | 3 +- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/pkg/configfilearg/parser.go b/pkg/configfilearg/parser.go index a8784388fec0..b367fd9a02aa 100644 --- a/pkg/configfilearg/parser.go +++ b/pkg/configfilearg/parser.go @@ -98,6 +98,7 @@ 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 last_val string if configFile != "" { _, err := os.Stat(configFile) @@ -122,17 +123,21 @@ func (p *Parser) FindString(args []string, target string) (string, error) { 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 { - return v, nil + if isAppend { + last_val = last_val + v + } else { + last_val = v + } } } } } - - return "", nil + return last_val, nil } func (p *Parser) findConfigFileFlag(args []string) (string, bool) { diff --git a/pkg/configfilearg/parser_test.go b/pkg/configfilearg/parser_test.go index 373e8b691a97..764217c9397d 100644 --- a/pkg/configfilearg/parser_test.go +++ b/pkg/configfilearg/parser_test.go @@ -384,9 +384,9 @@ func Test_UnitParser_FindString(t *testing.T) { }, args: args{ osArgs: []string{"-c", "./testdata/data.yaml"}, - target: "foo-bar", + target: "alice", }, - want: "baz", + want: "bob", }, { name: "A custom config exists, target does not exist", @@ -408,11 +408,37 @@ func Test_UnitParser_FindString(t *testing.T) { 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", + want: "onetwo", }, } for _, tt := range tests { diff --git a/pkg/configfilearg/testdata/data.yaml b/pkg/configfilearg/testdata/data.yaml index 35105341ad7a..3e8d5e9f5da0 100644 --- a/pkg/configfilearg/testdata/data.yaml +++ b/pkg/configfilearg/testdata/data.yaml @@ -1,4 +1,4 @@ -foo-bar: baz +alice: bob a-slice: - 1 - "2" diff --git a/pkg/configfilearg/testdata/data.yaml.d/01-data.yml b/pkg/configfilearg/testdata/data.yaml.d/01-data.yml index d118ca5a00f7..9a0a098f7c73 100644 --- a/pkg/configfilearg/testdata/data.yaml.d/01-data.yml +++ b/pkg/configfilearg/testdata/data.yaml.d/01-data.yml @@ -11,4 +11,5 @@ c-slice: - two d-slice: - one -- two \ No newline at end of file +- two +f-string: beta \ No newline at end of file From fc18b5609e22185566e73908ca52cbe6129d1277 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Thu, 20 Apr 2023 09:00:47 -0700 Subject: [PATCH 3/5] Fix append case Signed-off-by: Derek Nola --- pkg/configfilearg/parser.go | 2 +- pkg/configfilearg/parser_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/configfilearg/parser.go b/pkg/configfilearg/parser.go index b367fd9a02aa..d84dce6b461d 100644 --- a/pkg/configfilearg/parser.go +++ b/pkg/configfilearg/parser.go @@ -129,7 +129,7 @@ func (p *Parser) FindString(args []string, target string) (string, error) { k = strings.TrimSuffix(k, "+") if k == target { if isAppend { - last_val = last_val + v + last_val = last_val + "," + v } else { last_val = v } diff --git a/pkg/configfilearg/parser_test.go b/pkg/configfilearg/parser_test.go index 764217c9397d..f9bdc75f3af4 100644 --- a/pkg/configfilearg/parser_test.go +++ b/pkg/configfilearg/parser_test.go @@ -438,7 +438,7 @@ func Test_UnitParser_FindString(t *testing.T) { osArgs: []string{"-c", "./testdata/data.yaml"}, target: "b-string", }, - want: "onetwo", + want: "one,two", }, } for _, tt := range tests { From 20e5649b3d8f19e8fd7cda5b283bae4b982086a1 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Thu, 20 Apr 2023 13:31:36 -0700 Subject: [PATCH 4/5] linter fix Signed-off-by: Derek Nola --- pkg/configfilearg/parser.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/configfilearg/parser.go b/pkg/configfilearg/parser.go index d84dce6b461d..f93e13911b83 100644 --- a/pkg/configfilearg/parser.go +++ b/pkg/configfilearg/parser.go @@ -98,7 +98,7 @@ 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 last_val string + var lastVal string if configFile != "" { _, err := os.Stat(configFile) @@ -129,15 +129,15 @@ func (p *Parser) FindString(args []string, target string) (string, error) { k = strings.TrimSuffix(k, "+") if k == target { if isAppend { - last_val = last_val + "," + v + lastVal = lastVal + "," + v } else { - last_val = v + lastVal = v } } } } } - return last_val, nil + return lastVal, nil } func (p *Parser) findConfigFileFlag(args []string) (string, bool) { From 780f2be676ee6ad9775f4f5d8d66de72fa3bdadd Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Thu, 20 Apr 2023 13:57:38 -0700 Subject: [PATCH 5/5] Fix test default check Signed-off-by: Derek Nola --- pkg/configfilearg/parser_test.go | 2 ++ pkg/configfilearg/testdata/data.yaml | 1 + 2 files changed, 3 insertions(+) diff --git a/pkg/configfilearg/parser_test.go b/pkg/configfilearg/parser_test.go index f9bdc75f3af4..66faa7586810 100644 --- a/pkg/configfilearg/parser_test.go +++ b/pkg/configfilearg/parser_test.go @@ -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", @@ -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", } diff --git a/pkg/configfilearg/testdata/data.yaml b/pkg/configfilearg/testdata/data.yaml index 3e8d5e9f5da0..712668e5ab31 100644 --- a/pkg/configfilearg/testdata/data.yaml +++ b/pkg/configfilearg/testdata/data.yaml @@ -1,3 +1,4 @@ +foo-bar: baz alice: bob a-slice: - 1