Skip to content

Commit

Permalink
Merge pull request #7977 from hashicorp/jbardin/ZD-1438
Browse files Browse the repository at this point in the history
numeric variables aren't always interpreted as str
  • Loading branch information
jbardin committed Aug 4, 2016
2 parents f75c3a9 + d5fbb5f commit 256190a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
10 changes: 8 additions & 2 deletions command/hcl_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ func encodeHCL(i interface{}) ([]byte, error) {
// now strip that first assignment off
eq := regexp.MustCompile(`=\s+`).FindIndex(hcl)

return hcl[eq[1]:], nil
// strip of an extra \n if it's there
end := len(hcl)
if hcl[end-1] == '\n' {
end -= 1
}

return hcl[eq[1]:end], nil
}

type encodeState struct {
Expand Down Expand Up @@ -107,7 +113,7 @@ func (e *encodeState) encodeInt(i interface{}) error {
}

func (e *encodeState) encodeFloat(f interface{}) error {
_, err := fmt.Fprintf(e, "%f", f)
_, err := fmt.Fprintf(e, "%g", f)
return err
}

Expand Down
14 changes: 2 additions & 12 deletions command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,25 +327,15 @@ RANGE:
case string:
tfv.Value = v

case []interface{}:
hcl, err = encodeHCL(v)
if err != nil {
break RANGE
}

tfv.Value = string(hcl)
tfv.IsHCL = true

case map[string]interface{}:
default:
// everything that's not a string is now HCL encoded
hcl, err = encodeHCL(v)
if err != nil {
break RANGE
}

tfv.Value = string(hcl)
tfv.IsHCL = true
default:
err = fmt.Errorf("unknown type %T for variable %s", v, k)
}

tfVars = append(tfVars, tfv)
Expand Down
16 changes: 12 additions & 4 deletions command/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ func TestPush_tfvars(t *testing.T) {
args := []string{
"-var-file", path + "/terraform.tfvars",
"-vcs=false",
"-var",
"bar=1",
path,
}
if code := c.Run(args); code != 0 {
Expand All @@ -412,12 +414,19 @@ func TestPush_tfvars(t *testing.T) {

//now check TFVars
tfvars := pushTFVars()
// update bar to match cli value
for i, v := range tfvars {
if v.Key == "bar" {
tfvars[i].Value = "1"
tfvars[i].IsHCL = true
}
}

for i, expected := range tfvars {
got := client.UpsertOptions.TFVars[i]
if got != expected {
t.Logf("%2d expected: %#v", i, expected)
t.Logf(" got: %#v", got)
t.Fatalf(" got: %#v", got)
}
}
}
Expand Down Expand Up @@ -589,9 +598,8 @@ func pushTFVars() []atlas.TFVar {
{"baz", `{
A = "a"
interp = "${file("t.txt")}"
}
`, true},
{"fob", `["a", "quotes \"in\" quotes"]` + "\n", true},
}`, true},
{"fob", `["a", "quotes \"in\" quotes"]`, true},
{"foo", "bar", false},
}
}
Expand Down

0 comments on commit 256190a

Please sign in to comment.