Skip to content

Commit 41a4235

Browse files
committed
terraformrc can contain env var references
This allows the use case where installation of a plugin can simply say to add `$GOPATH/bin/foo` to your terraformrc and the user can do that verbatim. Additionally terraformrc files become portable for certain environments which are self-contained. I was hesitant at first about this because it diverges the syntax a bit from our standard interpolation syntax. However, due to the special nature of terraformrc and the strong use cases cited I'm okay with this.
1 parent 3e2f324 commit 41a4235

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

config.go

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ func LoadConfig(path string) (*Config, error) {
7474
return nil, err
7575
}
7676

77+
// Replace all env vars
78+
for k, v := range result.Providers {
79+
result.Providers[k] = os.ExpandEnv(v)
80+
}
81+
for k, v := range result.Provisioners {
82+
result.Provisioners[k] = os.ExpandEnv(v)
83+
}
84+
7785
return &result, nil
7886
}
7987

config_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"os"
45
"path/filepath"
56
"reflect"
67
"testing"
@@ -27,6 +28,30 @@ func TestLoadConfig(t *testing.T) {
2728
}
2829
}
2930

31+
func TestLoadConfig_env(t *testing.T) {
32+
defer os.Unsetenv("TFTEST")
33+
os.Setenv("TFTEST", "hello")
34+
35+
c, err := LoadConfig(filepath.Join(fixtureDir, "config-env"))
36+
if err != nil {
37+
t.Fatalf("err: %s", err)
38+
}
39+
40+
expected := &Config{
41+
Providers: map[string]string{
42+
"aws": "hello",
43+
"google": "bar",
44+
},
45+
Provisioners: map[string]string{
46+
"local": "hello",
47+
},
48+
}
49+
50+
if !reflect.DeepEqual(c, expected) {
51+
t.Fatalf("bad: %#v", c)
52+
}
53+
}
54+
3055
func TestConfig_Merge(t *testing.T) {
3156
c1 := &Config{
3257
Providers: map[string]string{

test-fixtures/config-env

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
providers {
2+
aws = "$TFTEST"
3+
google = "bar"
4+
}
5+
6+
provisioners {
7+
local = "$TFTEST"
8+
}

0 commit comments

Comments
 (0)