packer: Ui.Ask quits during interrupt
This commit is contained in:
parent
21b6d2a435
commit
9a69b21025
18
packer/ui.go
18
packer/ui.go
|
@ -4,6 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -101,6 +103,10 @@ func (rw *ReaderWriterUi) Ask(query string) string {
|
||||||
rw.l.Lock()
|
rw.l.Lock()
|
||||||
defer rw.l.Unlock()
|
defer rw.l.Unlock()
|
||||||
|
|
||||||
|
sigCh := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigCh, os.Interrupt)
|
||||||
|
defer signal.Stop(sigCh)
|
||||||
|
|
||||||
log.Printf("ui: ask: %s", query)
|
log.Printf("ui: ask: %s", query)
|
||||||
if query != "" {
|
if query != "" {
|
||||||
if _, err := fmt.Fprint(rw.Writer, query+" "); err != nil {
|
if _, err := fmt.Fprint(rw.Writer, query+" "); err != nil {
|
||||||
|
@ -108,12 +114,24 @@ func (rw *ReaderWriterUi) Ask(query string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result := make(chan string, 1)
|
||||||
|
go func() {
|
||||||
var line string
|
var line string
|
||||||
if _, err := fmt.Fscanln(rw.Reader, &line); err != nil {
|
if _, err := fmt.Fscanln(rw.Reader, &line); err != nil {
|
||||||
log.Printf("ui: scan err: %s", err)
|
log.Printf("ui: scan err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result <- line
|
||||||
|
}()
|
||||||
|
|
||||||
|
select{
|
||||||
|
case line := <-result:
|
||||||
return line
|
return line
|
||||||
|
case <-sigCh:
|
||||||
|
log.Println("Interrupt during Ask call. Returning immediately.")
|
||||||
|
fmt.Fprintln(rw.Writer)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *ReaderWriterUi) Say(message string) {
|
func (rw *ReaderWriterUi) Say(message string) {
|
||||||
|
|
Loading…
Reference in New Issue