command: autoload terraform.tfvars.json as well [GH-1030]

This commit is contained in:
Mitchell Hashimoto 2015-03-02 09:21:45 -08:00
parent 8e76a02a56
commit 71315076c3
2 changed files with 64 additions and 0 deletions

View File

@ -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) { func TestApply_backup(t *testing.T) {
originalState := &terraform.State{ originalState := &terraform.State{
Modules: []*terraform.ModuleState{ Modules: []*terraform.ModuleState{
@ -1150,6 +1202,10 @@ const applyVarFile = `
foo = "bar" foo = "bar"
` `
const applyVarFileJSON = `
{ "foo": "bar" }
`
const testApplyDisableBackupStr = ` const testApplyDisableBackupStr = `
ID = bar ID = bar
` `

View File

@ -346,6 +346,14 @@ func (m *Meta) process(args []string, vars bool) []string {
args[0] = "-" + m.autoKey args[0] = "-" + m.autoKey
args[1] = DefaultVarsFilename 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 return args