cmd/utils: Rewrite askForConfirmation() using askForConfirmationAsync()

This is meant to avoid duplicating the code that shows the prompt, and
reads and parses the user's input.

https://github.com/containers/toolbox/issues/752
https://github.com/containers/toolbox/issues/1263
This commit is contained in:
Debarshi Ray 2023-12-06 12:44:23 +01:00
parent 6e538284ad
commit 2a66fd31de
1 changed files with 8 additions and 22 deletions

View File

@ -55,29 +55,15 @@ var (
func askForConfirmation(prompt string) bool {
var retVal bool
for {
fmt.Printf("%s ", prompt)
ctx := context.Background()
retValCh, errCh := askForConfirmationAsync(ctx, prompt, nil)
var response string
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
if scanner.Scan() {
response = scanner.Text()
}
if response == "" {
response = "n"
} else {
response = strings.ToLower(response)
}
if response == "no" || response == "n" {
break
} else if response == "yes" || response == "y" {
retVal = true
break
}
select {
case val := <-retValCh:
retVal = val
case err := <-errCh:
logrus.Debugf("Failed to ask for confirmation: %s", err)
retVal = false
}
return retVal