packer: Ui.Ask quits during interrupt

This commit is contained in:
Mitchell Hashimoto 2013-06-14 15:59:54 -07:00
parent 21b6d2a435
commit 9a69b21025
1 changed files with 23 additions and 5 deletions

View File

@ -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) {