-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinput.go
107 lines (97 loc) · 2.72 KB
/
input.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package interact
import (
"errors"
"fmt"
"strings"
)
var (
errCanceled = errors.New("Command aborted")
)
// InputCheck specifies the function signature for an input check
type InputCheck func(string) error
// PromptAndRetry asks the user for input and performs the list of added checks
// on the provided input. If any of the checks fail to pass the error will be
// displayed to the user and they will then be asked if they want to try again.
// If the user does not want to retry the program will return an error.
func (a Actor) PromptAndRetry(message string, checks ...InputCheck) (string, error) {
for {
input, err := a.Prompt(message, checks...)
if err != nil {
if err = a.confirmRetry(err); err != nil {
return "", err
}
continue
}
return input, nil
}
}
// PromptOptionalAndRetry works exactly like GetInputAndRetry, but also has
// a default option which will be used instead if the user simply presses enter.
func (a Actor) PromptOptionalAndRetry(message, defaultOption string, checks ...InputCheck) (string, error) {
for {
input, err := a.PromptOptional(message, defaultOption, checks...)
if err != nil {
if err = a.confirmRetry(err); err != nil {
return "", err
}
continue
}
return input, nil
}
}
// Prompt asks the user for input and performs the list of added checks on the
// provided input. If any of the checks fail, the error will be returned.
func (a Actor) Prompt(message string, checks ...InputCheck) (string, error) {
input, err := a.prompt(message + ": ")
if err != nil {
return "", err
}
err = runChecks(input, checks...)
if err != nil {
return "", err
}
return input, nil
}
// PromptOptional works exactly like Prompt, but also has a default option
// which will be used instead if the user simply presses enter.
func (a Actor) PromptOptional(message, defaultOption string, checks ...InputCheck) (string, error) {
input, err := a.prompt(fmt.Sprintf("%s: (%s) ", message, defaultOption))
if err != nil {
return "", err
}
if input == "" {
return defaultOption, nil
}
err = runChecks(input, checks...)
if err != nil {
return "", err
}
return input, nil
}
func (a Actor) confirmRetry(err error) error {
retryMessage := fmt.Sprintf("%v\nDo you want to try again?", err)
confirmed, err := a.Confirm(retryMessage, ConfirmDefaultToNo)
if err != nil {
return err
} else if !confirmed {
return errCanceled
}
return nil
}
func (a Actor) prompt(message string) (string, error) {
fmt.Fprint(a.w, message)
line, err := a.rd.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(line), nil
}
func runChecks(input string, checks ...InputCheck) error {
for _, check := range checks {
err := check(input)
if err != nil {
return err
}
}
return nil
}