diff --git a/config/append.go b/config/append.go index a421df4a0..5f4e89eef 100644 --- a/config/append.go +++ b/config/append.go @@ -35,8 +35,13 @@ func Append(c1, c2 *Config) (*Config, error) { c.Atlas = c2.Atlas } - c.Terraform = c1.Terraform - if c2.Terraform != nil { + // merge Terraform blocks + if c1.Terraform != nil { + c.Terraform = c1.Terraform + if c2.Terraform != nil { + c.Terraform.Merge(c2.Terraform) + } + } else { c.Terraform = c2.Terraform } diff --git a/config/append_test.go b/config/append_test.go index aecb80e66..8c81ed91d 100644 --- a/config/append_test.go +++ b/config/append_test.go @@ -118,6 +118,30 @@ func TestAppend(t *testing.T) { }, false, }, + + { + &Config{ + Terraform: &Terraform{ + RequiredVersion: "A", + }, + }, + &Config{ + Terraform: &Terraform{ + Backend: &Backend{ + Type: "test", + }, + }, + }, + &Config{ + Terraform: &Terraform{ + RequiredVersion: "A", + Backend: &Backend{ + Type: "test", + }, + }, + }, + false, + }, } for i, tc := range cases { diff --git a/config/config_terraform.go b/config/config_terraform.go index 952d59cc4..a547cc798 100644 --- a/config/config_terraform.go +++ b/config/config_terraform.go @@ -47,6 +47,18 @@ func (t *Terraform) Validate() []error { return errs } +// Merge t with t2. +// Any conflicting fields are overwritten by t2. +func (t *Terraform) Merge(t2 *Terraform) { + if t2.RequiredVersion != "" { + t.RequiredVersion = t2.RequiredVersion + } + + if t2.Backend != nil { + t.Backend = t2.Backend + } +} + // Backend is the configuration for the "backend" to use with Terraform. // A backend is responsible for all major behavior of Terraform's core. // The abstraction layer above the core (the "backend") allows for behavior