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.
This commit is contained in:
Mitchell Hashimoto 2017-02-13 17:52:51 -08:00
parent 3e2f324b20
commit 41a4235eb3
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 41 additions and 0 deletions

View File

@ -74,6 +74,14 @@ func LoadConfig(path string) (*Config, error) {
return nil, err 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 return &result, nil
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
@ -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) { func TestConfig_Merge(t *testing.T) {
c1 := &Config{ c1 := &Config{
Providers: map[string]string{ Providers: map[string]string{

8
test-fixtures/config-env Normal file
View File

@ -0,0 +1,8 @@
providers {
aws = "$TFTEST"
google = "bar"
}
provisioners {
local = "$TFTEST"
}