command: use bufio.ReadString instead of scanning to get spaces

[GH-2628]
This commit is contained in:
Mitchell Hashimoto 2016-08-22 12:59:48 -07:00
parent 29e6570df8
commit 2ac142abc3
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 21 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import (
"os/signal" "os/signal"
"strings" "strings"
"sync" "sync"
"unicode"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/colorstring" "github.com/mitchellh/colorstring"
@ -95,12 +96,13 @@ func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) {
// interrupt this if we are interrupted (SIGINT) // interrupt this if we are interrupted (SIGINT)
result := make(chan string, 1) result := make(chan string, 1)
go func() { go func() {
var line string buf := bufio.NewReader(r)
if _, err := fmt.Fscanln(r, &line); err != nil { line, err := buf.ReadString('\n')
if err != nil {
log.Printf("[ERR] UIInput scan err: %s", err) log.Printf("[ERR] UIInput scan err: %s", err)
} }
result <- line result <- strings.TrimRightFunc(line, unicode.IsSpace)
}() }()
select { select {

View File

@ -26,3 +26,19 @@ func TestUIInputInput(t *testing.T) {
t.Fatalf("bad: %#v", v) 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)
}
}