init tty in main

This commit is contained in:
Adrien Delorme 2019-03-06 15:52:59 +01:00
parent 22df491c67
commit 94d6fc10f5
2 changed files with 11 additions and 10 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/hashicorp/packer/packer/plugin"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/version"
"github.com/mattn/go-tty"
"github.com/mitchellh/cli"
"github.com/mitchellh/panicwrap"
"github.com/mitchellh/prefixedio"
@ -185,11 +186,17 @@ func wrappedMain() int {
defer plugin.CleanupClients()
tty, err := tty.Open()
if err != nil {
log.Printf("running packer without a tty: %s", err)
}
// Setup the UI if we're being machine-readable
var ui packer.Ui = &packer.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stdout,
TTY: tty,
}
if machineReadable {
ui = &packer.MachineReadableUi{

View File

@ -14,8 +14,6 @@ import (
"syscall"
"time"
"unicode"
"github.com/mattn/go-tty"
)
type UiColor uint
@ -82,7 +80,7 @@ type BasicUi struct {
ErrorWriter io.Writer
l sync.Mutex
interrupted bool
tty TTY
TTY TTY
StackableProgressBar
}
@ -210,12 +208,8 @@ func (rw *BasicUi) Ask(query string) (string, error) {
return "", errors.New("interrupted")
}
if rw.tty == nil {
var err error
rw.tty, err = tty.Open()
if err != nil {
return "", fmt.Errorf("tty open: %s", err)
}
if rw.TTY == nil {
return "", errors.New("no available tty")
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
@ -230,7 +224,7 @@ func (rw *BasicUi) Ask(query string) (string, error) {
result := make(chan string, 1)
go func() {
line, err := rw.tty.ReadString()
line, err := rw.TTY.ReadString()
if err != nil {
log.Printf("ui: scan err: %s", err)
return