Skip to content

Commit

Permalink
Merge pull request #1093 from hashicorp/f-autoload-tfvars-json
Browse files Browse the repository at this point in the history
command: autoload terraform.tfvars.json as well [GH-1030]
  • Loading branch information
mitchellh committed Mar 2, 2015
2 parents 2450b03 + 7131507 commit 50da2bd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
56 changes: 56 additions & 0 deletions command/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,58 @@ func TestApply_varFileDefault(t *testing.T) {
}
}

func TestApply_varFileDefaultJSON(t *testing.T) {
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars.json")
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFileJSON), 0644); err != nil {
t.Fatalf("err: %s", err)
}

statePath := testTempFile(t)

cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(varFileDir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)

p := testProvider()
ui := new(cli.MockUi)
c := &ApplyCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

actual := ""
p.DiffFn = func(
info *terraform.InstanceInfo,
s *terraform.InstanceState,
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}

return &terraform.InstanceDiff{}, nil
}

args := []string{
"-state", statePath,
testFixturePath("apply-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

if actual != "bar" {
t.Fatal("didn't work")
}
}

func TestApply_backup(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
Expand Down Expand Up @@ -1150,6 +1202,10 @@ const applyVarFile = `
foo = "bar"
`

const applyVarFileJSON = `
{ "foo": "bar" }
`

const testApplyDisableBackupStr = `
ID = bar
`
Expand Down
8 changes: 8 additions & 0 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ func (m *Meta) process(args []string, vars bool) []string {
args[0] = "-" + m.autoKey
args[1] = DefaultVarsFilename
}

if _, err := os.Stat(DefaultVarsFilename + ".json"); err == nil {
m.autoKey = "var-file-default"
args = append(args, "", "")
copy(args[2:], args[0:])
args[0] = "-" + m.autoKey
args[1] = DefaultVarsFilename + ".json"
}
}

return args
Expand Down

0 comments on commit 50da2bd

Please sign in to comment.