command: FlagTypedKV parses bool as string

When passing a bool type to a variable such as `-var foo=true`, the CLI
would parse this as a `bool` type which Terraform core cannot handle.
It would then error with an invalid type error.

This changes the handling to convert the bool to its literally string
value given on the command-line.
This commit is contained in:
Mitchell Hashimoto 2016-10-26 21:45:39 -04:00
parent aed23a0a31
commit f9b0207304
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 14 additions and 0 deletions

View File

@ -166,6 +166,7 @@ func parseVarFlagAsHCL(input string) (string, interface{}, error) {
if _, err := strconv.ParseFloat(trimmed, 64); err == nil { if _, err := strconv.ParseFloat(trimmed, 64); err == nil {
return probablyName, value, nil return probablyName, value, nil
} }
// HCL will also parse hex as a number // HCL will also parse hex as a number
if strings.HasPrefix(trimmed, "0x") { if strings.HasPrefix(trimmed, "0x") {
if _, err := strconv.ParseInt(trimmed[2:], 16, 64); err == nil { if _, err := strconv.ParseInt(trimmed[2:], 16, 64); err == nil {
@ -173,6 +174,13 @@ func parseVarFlagAsHCL(input string) (string, interface{}, error) {
} }
} }
// If the value is a boolean value, also convert it to a simple string
// since Terraform core doesn't accept primitives as anything other
// than string for now.
if _, err := strconv.ParseBool(trimmed); err == nil {
return probablyName, value, nil
}
parsed, err := hcl.Parse(input) parsed, err := hcl.Parse(input)
if err != nil { if err != nil {
// 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

View File

@ -98,6 +98,12 @@ func TestFlagTypedKV(t *testing.T) {
false, false,
}, },
{
"key=false",
map[string]interface{}{"key": "false"},
false,
},
{ {
"map.key=foo", "map.key=foo",
map[string]interface{}{"map.key": "foo"}, map[string]interface{}{"map.key": "foo"},