summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaddy Carey <pcarey@cloudsmith.io>2022-03-21 19:18:50 +0000
committerDave Henderson <dhenderson@gmail.com>2022-04-27 13:56:43 -0400
commitdf96eb387435e7b540b04d38dffc5c4f737c8cfa (patch)
treed6e3f0e471c8a1cfb31637501c2a8fc255200157
parent46e6e9ccc4164116af0b43cba120274e76a1d592 (diff)
Fix merging context/datasources config
-rw-r--r--internal/config/configfile.go12
-rw-r--r--internal/config/configfile_test.go55
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) {