From 2a66fd31de414ebb80112abc2f6a7c0f1c440692 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Wed, 6 Dec 2023 12:44:23 +0100 Subject: [PATCH] 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 --- src/cmd/utils.go | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/cmd/utils.go b/src/cmd/utils.go index 7b51d10..92b5a42 100644 --- a/src/cmd/utils.go +++ b/src/cmd/utils.go @@ -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