blob: 773b52980b17f4e2266df008466f38cbee534663 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package utils
import (
"bufio"
"fmt"
"os"
"strings"
)
func AskUserYesNo(prompt string, defaultVal bool) (bool, error) {
suffix := "[y/N]"
if defaultVal {
suffix = "[Y/n]"
}
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("%s %s ", prompt, suffix)
scanner.Scan()
if err := scanner.Err(); err != nil {
return false, fmt.Errorf("failed to read confirmation: %w", err)
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
return defaultVal, nil
}
switch strings.ToUpper(input) {
case "Y", "YE", "YES":
return true, nil
default:
return false, nil
}
}
|