From 74f3ad2220bde862113213dcdbb688a8190adef9 Mon Sep 17 00:00:00 2001 From: Kyle Hodgetts Date: Tue, 14 Sep 2021 13:06:50 +0300 Subject: [PATCH] Check for interrupts in prompter. Exit immediately if interrupt detected (#160) --- wizard/prompt/prompt.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/wizard/prompt/prompt.go b/wizard/prompt/prompt.go index 89fd210..513a040 100644 --- a/wizard/prompt/prompt.go +++ b/wizard/prompt/prompt.go @@ -37,7 +37,10 @@ func (pr prompter) SelectOneOf(label string, variants []string, withAdd bool) st Items: variants, } - _, res, _ := p.Run() + _, res, err := p.Run() + if errors.Is(err, promptui.ErrInterrupt) { + exit() + } return res } @@ -47,7 +50,10 @@ func (pr prompter) SelectOneOf(label string, variants []string, withAdd bool) st Items: variants, } - _, res, _ := p.Run() + _, res, err := p.Run() + if errors.Is(err, promptui.ErrInterrupt) { + exit() + } return res } @@ -61,7 +67,10 @@ func (_ prompter) Input(label, defaultString string) string { Default: defaultString, } - res, _ := p.Run() + res, err := p.Run() + if errors.Is(err, promptui.ErrInterrupt) { + exit() + } return res } @@ -80,7 +89,10 @@ func (_ prompter) InputNonEmpty(label, defaultString string) string { Default: defaultString, } - res, _ := p.Run() + res, err := p.Run() + if errors.Is(err, promptui.ErrInterrupt) { + exit() + } return res } @@ -123,7 +135,10 @@ func (_ prompter) FilePath(label, defaultPath string, shouldExist bool) string { }, } - res, _ := p.Run() + res, err := p.Run() + if errors.Is(err, promptui.ErrInterrupt) { + exit() + } return res } @@ -137,14 +152,22 @@ func (_ prompter) Confirm(question string) bool { _, err := p.Run() if err != nil { - if errors.Is(err, promptui.ErrAbort) { + switch err { + case promptui.ErrAbort: return false + case promptui.ErrInterrupt: + exit() } } return true } +func exit() { + fmt.Println("Exiting.") + os.Exit(1) +} + func fileExists(path string) bool { // check if file exists f, err := os.Stat(path)