From f9b02073049f180d87a9cd212c78181f134bbaa2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 26 Oct 2016 21:45:39 -0400 Subject: [PATCH] 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. --- command/flag_kv.go | 8 ++++++++ command/flag_kv_test.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/command/flag_kv.go b/command/flag_kv.go index a618001df..8d73e43b0 100644 --- a/command/flag_kv.go +++ b/command/flag_kv.go @@ -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 diff --git a/command/flag_kv_test.go b/command/flag_kv_test.go index de5dd9d84..d42c88d36 100644 --- a/command/flag_kv_test.go +++ b/command/flag_kv_test.go @@ -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"},