Skip to content

Commit

Permalink
terraformrc can contain env var references
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mitchellh committed Feb 14, 2017
1 parent 3e2f324 commit 41a4235
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 41a4235

Please sign in to comment.