-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
60 lines (53 loc) · 1.19 KB
/
prompt.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"strings"
)
func promptYesNo(msg string) (bool, error) {
// Bracket the prompt in a horizontal rule
fmt.Printf("----\n")
defer fmt.Printf("----\n")
// Prompt the user
fmt.Printf("%s [yes/no]\n", msg)
// Collect a response
r := bufio.NewReader(os.Stdin)
response, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
// The reader closed, probably due to CTRL+C
return false, context.Canceled
}
return false, err
}
// Validate the response
response = strings.ToLower(strings.TrimSpace(response))
switch response {
case "yes":
return true, nil
case "no", "n":
return false, nil
default:
return false, fmt.Errorf("\"%s\" is not \"yes\" or \"no\"", response)
}
}
func promptEnter(msg string) (bool, error) {
// Bracket the prompt in a horizontal rule
fmt.Printf("----\n")
defer fmt.Printf("----\n")
// Collect a response
fmt.Printf("%s [press enter to continue or CTRL+C to exit]\n", msg)
r := bufio.NewReader(os.Stdin)
_, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
// The reader closed, probably due to CTRL+C
return false, nil
}
return false, err
}
return true, nil
}