Skip to content

Commit

Permalink
Check for interrupts in prompter. Exit immediately if interrupt detec…
Browse files Browse the repository at this point in the history
…ted (#160)
  • Loading branch information
Kyle Hodgetts committed Sep 14, 2021
1 parent 8e08040 commit 74f3ad2
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions wizard/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 74f3ad2

Please sign in to comment.