Commit 9a69b210 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Ui.Ask quits during interrupt

parent 21b6d2a4
...@@ -4,6 +4,8 @@ import ( ...@@ -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 { ...@@ -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 { ...@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment