From b7f6f8eb2aa0e09e9abe38f8d74d36c0a2491220 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Dec 2016 20:55:10 -0800 Subject: [PATCH 1/2] helper/variables: trim whitespace around the key in -var Fixes #10716 This trims whitespace around the key in the `-var` flag. This is a regression from 0.7.x. The value is whitespace sensitive unless double-quoted. This is the same behavior as 0.7.x. I considered rejecting whitespace around the '=' completely but I don't want to introduce BC and the behavior actually seems quite obvious to me. --- helper/variables/flag.go | 7 +++++++ helper/variables/flag_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/helper/variables/flag.go b/helper/variables/flag.go index 393586234..497b6fb1e 100644 --- a/helper/variables/flag.go +++ b/helper/variables/flag.go @@ -21,11 +21,18 @@ func (v *Flag) Set(raw string) error { } key, input := raw[0:idx], raw[idx+1:] + if key == "" { + return fmt.Errorf("No key to left '=' in arg: %s", raw) + } + value, err := ParseInput(input) if err != nil { return err } + // Trim the whitespace on the key + key = strings.TrimSpace(key) + *v = Merge(*v, map[string]interface{}{key: value}) return nil } diff --git a/helper/variables/flag_test.go b/helper/variables/flag_test.go index f88b42cd1..66c8d1a65 100644 --- a/helper/variables/flag_test.go +++ b/helper/variables/flag_test.go @@ -19,6 +19,12 @@ func TestFlag(t *testing.T) { Output map[string]interface{} Error bool }{ + { + "=value", + nil, + true, + }, + { "key=value", map[string]interface{}{"key": "value"}, @@ -43,6 +49,24 @@ func TestFlag(t *testing.T) { false, }, + { + "key =value", + map[string]interface{}{"key": "value"}, + false, + }, + + { + "key = value", + map[string]interface{}{"key": " value"}, + false, + }, + + { + `key = "value"`, + map[string]interface{}{"key": "value"}, + false, + }, + { "map.key=foo", map[string]interface{}{"map.key": "foo"}, From 6d594b3bc6cd87dc2a35076a05f98cf8adc47749 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 14 Dec 2016 13:52:34 -0800 Subject: [PATCH 2/2] helper/variables: trim whitespace on key before empty string comp --- helper/variables/flag.go | 6 +++--- helper/variables/flag_test.go | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/helper/variables/flag.go b/helper/variables/flag.go index 497b6fb1e..3af1d9164 100644 --- a/helper/variables/flag.go +++ b/helper/variables/flag.go @@ -21,6 +21,9 @@ func (v *Flag) Set(raw string) error { } key, input := raw[0:idx], raw[idx+1:] + + // Trim the whitespace on the key + key = strings.TrimSpace(key) if key == "" { return fmt.Errorf("No key to left '=' in arg: %s", raw) } @@ -30,9 +33,6 @@ func (v *Flag) Set(raw string) error { return err } - // Trim the whitespace on the key - key = strings.TrimSpace(key) - *v = Merge(*v, map[string]interface{}{key: value}) return nil } diff --git a/helper/variables/flag_test.go b/helper/variables/flag_test.go index 66c8d1a65..1bd5471ae 100644 --- a/helper/variables/flag_test.go +++ b/helper/variables/flag_test.go @@ -25,6 +25,12 @@ func TestFlag(t *testing.T) { true, }, + { + " =value", + nil, + true, + }, + { "key=value", map[string]interface{}{"key": "value"},