Skip to content

Commit

Permalink
cmd/utils: Rewrite askForConfirmation() using askForConfirmationAsync()
Browse files Browse the repository at this point in the history
This is meant to avoid duplicating the code that shows the prompt, and
reads and parses the user's input.

#752
#1263
  • Loading branch information
debarshiray committed Dec 6, 2023
1 parent 4de06b3 commit 9da3b7f
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,15 @@ var (
func askForConfirmation(prompt string) bool {
var retVal bool

for {
fmt.Printf("%s ", prompt)

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
}
ctx := context.Background()
retValCh, errCh := askForConfirmationAsync(ctx, prompt, false)

select {
case val := <-retValCh:
retVal = val
case err := <-errCh:
logrus.Debugf("Failed to ask for confirmation: %s", err)
retVal = false
}

return retVal
Expand Down

0 comments on commit 9da3b7f

Please sign in to comment.