use go-tty in ui.Ask to fix #7299

This commit is contained in:
Adrien Delorme 2019-02-27 16:56:25 +01:00
parent c588a8a24d
commit 239a0c633f
1 changed files with 11 additions and 9 deletions

View File

@ -1,7 +1,6 @@
package packer package packer
import ( import (
"bufio"
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
@ -15,6 +14,8 @@ import (
"syscall" "syscall"
"time" "time"
"unicode" "unicode"
"github.com/mattn/go-tty"
) )
type UiColor uint type UiColor uint
@ -81,7 +82,7 @@ type BasicUi struct {
ErrorWriter io.Writer ErrorWriter io.Writer
l sync.Mutex l sync.Mutex
interrupted bool interrupted bool
scanner *bufio.Scanner tty *tty.TTY
StackableProgressBar StackableProgressBar
} }
@ -209,8 +210,12 @@ func (rw *BasicUi) Ask(query string) (string, error) {
return "", errors.New("interrupted") return "", errors.New("interrupted")
} }
if rw.scanner == nil { if rw.tty == nil {
rw.scanner = bufio.NewScanner(rw.Reader) var err error
rw.tty, err = tty.Open()
if err != nil {
return "", fmt.Errorf("tty open: %s", err)
}
} }
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
@ -225,11 +230,8 @@ func (rw *BasicUi) Ask(query string) (string, error) {
result := make(chan string, 1) result := make(chan string, 1)
go func() { go func() {
var line string line, err := rw.tty.ReadString()
if rw.scanner.Scan() { if err != nil {
line = rw.scanner.Text()
}
if err := rw.scanner.Err(); err != nil {
log.Printf("ui: scan err: %s", err) log.Printf("ui: scan err: %s", err)
return return
} }