init tty in main
This commit is contained in:
parent
22df491c67
commit
94d6fc10f5
7
main.go
7
main.go
@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/hashicorp/packer/packer/plugin"
|
"github.com/hashicorp/packer/packer/plugin"
|
||||||
"github.com/hashicorp/packer/packer/tmp"
|
"github.com/hashicorp/packer/packer/tmp"
|
||||||
"github.com/hashicorp/packer/version"
|
"github.com/hashicorp/packer/version"
|
||||||
|
"github.com/mattn/go-tty"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"github.com/mitchellh/panicwrap"
|
"github.com/mitchellh/panicwrap"
|
||||||
"github.com/mitchellh/prefixedio"
|
"github.com/mitchellh/prefixedio"
|
||||||
@ -185,11 +186,17 @@ func wrappedMain() int {
|
|||||||
|
|
||||||
defer plugin.CleanupClients()
|
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
|
// Setup the UI if we're being machine-readable
|
||||||
var ui packer.Ui = &packer.BasicUi{
|
var ui packer.Ui = &packer.BasicUi{
|
||||||
Reader: os.Stdin,
|
Reader: os.Stdin,
|
||||||
Writer: os.Stdout,
|
Writer: os.Stdout,
|
||||||
ErrorWriter: os.Stdout,
|
ErrorWriter: os.Stdout,
|
||||||
|
TTY: tty,
|
||||||
}
|
}
|
||||||
if machineReadable {
|
if machineReadable {
|
||||||
ui = &packer.MachineReadableUi{
|
ui = &packer.MachineReadableUi{
|
||||||
|
14
packer/ui.go
14
packer/ui.go
@ -14,8 +14,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/mattn/go-tty"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UiColor uint
|
type UiColor uint
|
||||||
@ -82,7 +80,7 @@ type BasicUi struct {
|
|||||||
ErrorWriter io.Writer
|
ErrorWriter io.Writer
|
||||||
l sync.Mutex
|
l sync.Mutex
|
||||||
interrupted bool
|
interrupted bool
|
||||||
tty TTY
|
TTY TTY
|
||||||
StackableProgressBar
|
StackableProgressBar
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,12 +208,8 @@ func (rw *BasicUi) Ask(query string) (string, error) {
|
|||||||
return "", errors.New("interrupted")
|
return "", errors.New("interrupted")
|
||||||
}
|
}
|
||||||
|
|
||||||
if rw.tty == nil {
|
if rw.TTY == nil {
|
||||||
var err error
|
return "", errors.New("no available tty")
|
||||||
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)
|
||||||
@ -230,7 +224,7 @@ func (rw *BasicUi) Ask(query string) (string, error) {
|
|||||||
|
|
||||||
result := make(chan string, 1)
|
result := make(chan string, 1)
|
||||||
go func() {
|
go func() {
|
||||||
line, err := rw.tty.ReadString()
|
line, err := rw.TTY.ReadString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ui: scan err: %s", err)
|
log.Printf("ui: scan err: %s", err)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user