packer: If interrupted, Ask is disabled

This commit is contained in:
Mitchell Hashimoto 2013-06-15 18:25:34 -07:00
parent 676041dc15
commit c1e7d4314f
1 changed files with 9 additions and 3 deletions

View File

@ -49,9 +49,10 @@ type PrefixedUi struct {
// The ReaderWriterUi is a UI that writes and reads from standard Go
// io.Reader and io.Writer.
type ReaderWriterUi struct {
Reader io.Reader
Writer io.Writer
l sync.Mutex
Reader io.Reader
Writer io.Writer
l sync.Mutex
interrupted bool
}
func (u *ColoredUi) Ask(query string) (string, error) {
@ -104,6 +105,10 @@ func (rw *ReaderWriterUi) Ask(query string) (string, error) {
rw.l.Lock()
defer rw.l.Unlock()
if rw.interrupted {
return "", errors.New("interrupted")
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
defer signal.Stop(sigCh)
@ -129,6 +134,7 @@ func (rw *ReaderWriterUi) Ask(query string) (string, error) {
case line := <-result:
return line, nil
case <-sigCh:
rw.interrupted = true
return "", errors.New("interrupted")
}
}