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