From 2ac142abc3341299e8124856167d0f1cc38e962e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 22 Aug 2016 12:59:48 -0700 Subject: [PATCH] command: use bufio.ReadString instead of scanning to get spaces [GH-2628] --- command/ui_input.go | 8 +++++--- command/ui_input_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/command/ui_input.go b/command/ui_input.go index 76f2f2264..bc666109d 100644 --- a/command/ui_input.go +++ b/command/ui_input.go @@ -11,6 +11,7 @@ import ( "os/signal" "strings" "sync" + "unicode" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/colorstring" @@ -95,12 +96,13 @@ func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) { // interrupt this if we are interrupted (SIGINT) result := make(chan string, 1) go func() { - var line string - if _, err := fmt.Fscanln(r, &line); err != nil { + buf := bufio.NewReader(r) + line, err := buf.ReadString('\n') + if err != nil { log.Printf("[ERR] UIInput scan err: %s", err) } - result <- line + result <- strings.TrimRightFunc(line, unicode.IsSpace) }() select { diff --git a/command/ui_input_test.go b/command/ui_input_test.go index b968d73b1..0da64a99c 100644 --- a/command/ui_input_test.go +++ b/command/ui_input_test.go @@ -26,3 +26,19 @@ func TestUIInputInput(t *testing.T) { t.Fatalf("bad: %#v", v) } } + +func TestUIInputInput_spaces(t *testing.T) { + i := &UIInput{ + Reader: bytes.NewBufferString("foo bar\n"), + Writer: bytes.NewBuffer(nil), + } + + v, err := i.Input(&terraform.InputOpts{}) + if err != nil { + t.Fatalf("err: %s", err) + } + + if v != "foo bar" { + t.Fatalf("bad: %#v", v) + } +}