Skip to content

Commit

Permalink
Merge pull request #11929 from hashicorp/f-terraformrc-env
Browse files Browse the repository at this point in the history
terraformrc can contain env var references
  • Loading branch information
mitchellh authored Feb 16, 2017
2 parents 7776af1 + 41a4235 commit d8e3fac
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func LoadConfig(path string) (*Config, error) {
return nil, err
}

// Replace all env vars
for k, v := range result.Providers {
result.Providers[k] = os.ExpandEnv(v)
}
for k, v := range result.Provisioners {
result.Provisioners[k] = os.ExpandEnv(v)
}

return &result, nil
}

Expand Down
25 changes: 25 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"path/filepath"
"reflect"
"testing"
Expand All @@ -27,6 +28,30 @@ func TestLoadConfig(t *testing.T) {
}
}

func TestLoadConfig_env(t *testing.T) {
defer os.Unsetenv("TFTEST")
os.Setenv("TFTEST", "hello")

c, err := LoadConfig(filepath.Join(fixtureDir, "config-env"))
if err != nil {
t.Fatalf("err: %s", err)
}

expected := &Config{
Providers: map[string]string{
"aws": "hello",
"google": "bar",
},
Provisioners: map[string]string{
"local": "hello",
},
}

if !reflect.DeepEqual(c, expected) {
t.Fatalf("bad: %#v", c)
}
}

func TestConfig_Merge(t *testing.T) {
c1 := &Config{
Providers: map[string]string{
Expand Down
8 changes: 8 additions & 0 deletions test-fixtures/config-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
providers {
aws = "$TFTEST"
google = "bar"
}

provisioners {
local = "$TFTEST"
}

0 comments on commit d8e3fac

Please sign in to comment.