From df96eb387435e7b540b04d38dffc5c4f737c8cfa Mon Sep 17 00:00:00 2001 From: Paddy Carey Date: Mon, 21 Mar 2022 19:18:50 +0000 Subject: Fix merging context/datasources config --- internal/config/configfile.go | 12 +++++++-- internal/config/configfile_test.go | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/internal/config/configfile.go b/internal/config/configfile.go index 459f9f9e..ccc16227 100644 --- a/internal/config/configfile.go +++ b/internal/config/configfile.go @@ -264,8 +264,16 @@ func (c *Config) MergeFrom(o *Config) *Config { if !isZero(o.Templates) { c.Templates = o.Templates } - mergeDataSources(c.DataSources, o.DataSources) - mergeDataSources(c.Context, o.Context) + if c.DataSources == nil { + c.DataSources = o.DataSources + } else { + mergeDataSources(c.DataSources, o.DataSources) + } + if c.Context == nil { + c.Context = o.Context + } else { + mergeDataSources(c.Context, o.Context) + } if len(o.Plugins) > 0 { for k, v := range o.Plugins { c.Plugins[k] = v diff --git a/internal/config/configfile_test.go b/internal/config/configfile_test.go index e22ddf94..6ab9efdf 100644 --- a/internal/config/configfile_test.go +++ b/internal/config/configfile_test.go @@ -329,6 +329,61 @@ func TestMergeFrom(t *testing.T) { } assert.EqualValues(t, expected, cfg.MergeFrom(other)) + + cfg = &Config{ + Input: "hello world", + OutMode: "644", + } + other = &Config{ + OutputFiles: []string{"out.txt"}, + Context: map[string]DataSource{ + "foo": { + URL: mustURL("https://example.com/foo.yaml"), + Header: http.Header{ + "Accept": {"application/json"}, + }, + }, + "bar": {URL: mustURL("stdin:///")}, + }, + DataSources: map[string]DataSource{ + "data": { + URL: mustURL("file:///data.json"), + }, + "moredata": { + URL: mustURL("https://example.com/more.json"), + Header: http.Header{ + "Authorization": {"Bearer abcd1234"}, + }, + }, + }, + } + expected = &Config{ + Input: "hello world", + OutputFiles: []string{"out.txt"}, + Context: map[string]DataSource{ + "foo": { + URL: mustURL("https://example.com/foo.yaml"), + Header: http.Header{ + "Accept": {"application/json"}, + }, + }, + "bar": {URL: mustURL("stdin:///")}, + }, + DataSources: map[string]DataSource{ + "data": { + URL: mustURL("file:///data.json"), + }, + "moredata": { + URL: mustURL("https://example.com/more.json"), + Header: http.Header{ + "Authorization": {"Bearer abcd1234"}, + }, + }, + }, + OutMode: "644", + } + + assert.EqualValues(t, expected, cfg.MergeFrom(other)) } func TestParseDataSourceFlags(t *testing.T) { -- cgit v1.2.3