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:
parent
aed23a0a31
commit
f9b0207304
|
@ -166,6 +166,7 @@ func parseVarFlagAsHCL(input string) (string, interface{}, error) {
|
|||
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 {
|
||||
|
@ -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)
|
||||
if err != nil {
|
||||
// If it didn't parse as HCL, we check if it doesn't match our
|
||||
|
|
|
@ -98,6 +98,12 @@ func TestFlagTypedKV(t *testing.T) {
|
|||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"key=false",
|
||||
map[string]interface{}{"key": "false"},
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"map.key=foo",
|
||||
map[string]interface{}{"map.key": "foo"},
|
||||
|
|
Loading…
Reference in New Issue