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