promptConfirm prints prompt and waits for the user to type y/yes or anything else. Returns true only when the user confirms with "y" or "yes" (case-insensitive).
(prompt string)
| 54 | // promptConfirm prints prompt and waits for the user to type y/yes or anything else. |
| 55 | // Returns true only when the user confirms with "y" or "yes" (case-insensitive). |
| 56 | func promptConfirm(prompt string) (bool, error) { |
| 57 | fmt.Printf("%s [y/N]: ", prompt) |
| 58 | var response string |
| 59 | _, err := fmt.Scanln(&response) |
| 60 | if err != nil { |
| 61 | // empty Enter is reported by fmt.Scanln as "unexpected newline" |
| 62 | if err.Error() == "unexpected newline" { |
| 63 | return false, nil |
| 64 | } |
| 65 | return false, err |
| 66 | } |
| 67 | response = strings.TrimSpace(strings.ToLower(response)) |
| 68 | return response == "y" || response == "yes", nil |
| 69 | } |