Merge pull request #9263 from hashicorp/jbardin/var-numbers

Don't parse a TestFlagTypedKV value as a number
This commit is contained in:
James Bardin 2016-10-13 14:07:46 -04:00 committed by GitHub
commit 95d6f29948
3 changed files with 47 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
@ -148,15 +149,31 @@ func parseVarFlagAsHCL(input string) (string, interface{}, error) {
return "", nil, fmt.Errorf("No '=' value in variable: %s", input) return "", nil, fmt.Errorf("No '=' value in variable: %s", input)
} }
probablyName := input[0:idx] probablyName := input[0:idx]
value := input[idx+1:]
trimmed := strings.TrimSpace(value)
// If the value is a simple number, don't parse it as hcl because the
// variable type may actually be a string, and HCL will convert it to the
// numberic value. We could check this in the validation later, but the
// conversion may alter the string value.
if _, err := strconv.ParseInt(trimmed, 10, 64); err == nil {
return probablyName, value, nil
}
if _, err := strconv.ParseFloat(trimmed, 64); err == nil {
return probablyName, value, nil
}
// HCL will also parse hex as a number
if strings.HasPrefix(trimmed, "0x") {
if _, err := strconv.ParseInt(trimmed[2:], 16, 64); err == nil {
return probablyName, value, nil
}
}
parsed, err := hcl.Parse(input) parsed, err := hcl.Parse(input)
if err != nil { if err != nil {
value := input[idx+1:]
// If it didn't parse as HCL, we check if it doesn't match our // If it didn't parse as HCL, we check if it doesn't match our
// whitelist of TF-accepted HCL types for inputs. If not, then // whitelist of TF-accepted HCL types for inputs. If not, then
// we let it through as a raw string. // we let it through as a raw string.
trimmed := strings.TrimSpace(value)
if !varFlagHCLRe.MatchString(trimmed) { if !varFlagHCLRe.MatchString(trimmed) {
return probablyName, value, nil return probablyName, value, nil
} }

View File

@ -2,10 +2,11 @@ package command
import ( import (
"flag" "flag"
"github.com/davecgh/go-spew/spew"
"io/ioutil" "io/ioutil"
"reflect" "reflect"
"testing" "testing"
"github.com/davecgh/go-spew/spew"
) )
func TestFlagStringKV_impl(t *testing.T) { func TestFlagStringKV_impl(t *testing.T) {
@ -145,6 +146,29 @@ func TestFlagTypedKV(t *testing.T) {
map[string]interface{}{"key": "1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef"}, map[string]interface{}{"key": "1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef"},
false, false,
}, },
// simple values that can parse as numbers should remain strings
{
"key=1",
map[string]interface{}{
"key": "1",
},
false,
},
{
"key=1.0",
map[string]interface{}{
"key": "1.0",
},
false,
},
{
"key=0x10",
map[string]interface{}{
"key": "0x10",
},
false,
},
} }
for _, tc := range cases { for _, tc := range cases {

View File

@ -559,7 +559,7 @@ func TestPush_tfvars(t *testing.T) {
"-var-file", path + "/terraform.tfvars", "-var-file", path + "/terraform.tfvars",
"-vcs=false", "-vcs=false",
"-var", "-var",
"bar=1", "bar=[1,2]",
path, path,
} }
if code := c.Run(args); code != 0 { if code := c.Run(args); code != 0 {
@ -586,7 +586,7 @@ func TestPush_tfvars(t *testing.T) {
// update bar to match cli value // update bar to match cli value
for i, v := range tfvars { for i, v := range tfvars {
if v.Key == "bar" { if v.Key == "bar" {
tfvars[i].Value = "1" tfvars[i].Value = "[1, 2]"
tfvars[i].IsHCL = true tfvars[i].IsHCL = true
} }
} }