forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ask.go
183 lines (159 loc) · 3.88 KB
/
ask.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package termio
import (
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
)
var (
// Stdout is exported for tests
Stdout io.Writer = os.Stdout
// Stdin is exported for tests
Stdin io.Reader = os.Stdin
// ErrAborted is returned if the user aborts an action
ErrAborted = fmt.Errorf("user aborted")
)
const (
maxTries = 42
)
// AskForString asks for a string once, using the default if the
// answer is empty. Errors are only returned on I/O errors
func AskForString(ctx context.Context, text, def string) (string, error) {
if ctxutil.IsAlwaysYes(ctx) || !ctxutil.IsInteractive(ctx) {
return def, nil
}
// check for context cancelation
select {
case <-ctx.Done():
return def, ErrAborted
default:
}
fmt.Fprintf(Stdout, "%s [%s]: ", text, def)
input, err := NewReader(ctx, Stdin).ReadLine()
if err != nil {
return "", fmt.Errorf("failed to read user input: %w", err)
}
input = strings.TrimSpace(input)
if input == "" {
input = def
}
return input, nil
}
// AskForBool ask for a bool (yes or no) exactly once.
// The empty answer uses the specified default, any other answer
// is an error.
func AskForBool(ctx context.Context, text string, def bool) (bool, error) {
if ctxutil.IsAlwaysYes(ctx) {
return def, nil
}
choices := "y/N/q"
if def {
choices = "Y/n/q"
}
str, err := AskForString(ctx, text, choices)
if err != nil {
return false, fmt.Errorf("failed to read user input: %w", err)
}
switch str {
case "Y/n/q":
return true, nil
case "y/N/q":
return false, nil
}
str = strings.ToLower(string(str[0]))
switch str {
case "y":
return true, nil
case "n":
return false, nil
case "q":
return false, ErrAborted
default:
return false, fmt.Errorf("unknown answer: %s", str)
}
}
// AskForInt asks for an valid interger once. If the input
// can not be converted to an int it returns an error
func AskForInt(ctx context.Context, text string, def int) (int, error) {
if ctxutil.IsAlwaysYes(ctx) {
return def, nil
}
str, err := AskForString(ctx, text+" (q to abort)", strconv.Itoa(def))
if err != nil {
return 0, err
}
if str == "q" {
return 0, ErrAborted
}
intVal, err := strconv.Atoi(str)
if err != nil {
return 0, fmt.Errorf("failed to convert to number: %w", err)
}
return intVal, nil
}
// AskForConfirmation asks a yes/no question until the user
// replies yes or no
func AskForConfirmation(ctx context.Context, text string) bool {
if ctxutil.IsAlwaysYes(ctx) {
return true
}
for i := 0; i < maxTries; i++ {
choice, err := AskForBool(ctx, text, false)
if err == nil {
return choice
}
if err == ErrAborted {
return false
}
}
return false
}
// AskForKeyImport asks for permissions to import the named key
func AskForKeyImport(ctx context.Context, key string, names []string) bool {
if ctxutil.IsAlwaysYes(ctx) {
return true
}
if !ctxutil.IsInteractive(ctx) {
return false
}
ok, err := AskForBool(ctx, fmt.Sprintf("Do you want to import the public key %q (Names: %+v) into your keyring?", key, names), false)
if err != nil {
return false
}
return ok
}
// AskForPassword prompts for a password, optionally prompting twice until both match
func AskForPassword(ctx context.Context, name string, repeat bool) (string, error) {
if ctxutil.IsAlwaysYes(ctx) {
return "", nil
}
askFn := GetPassPromptFunc(ctx)
for i := 0; i < maxTries; i++ {
// check for context cancellation
select {
case <-ctx.Done():
return "", ErrAborted
default:
}
pass, err := askFn(ctx, fmt.Sprintf("Enter %s", name))
if !repeat {
return pass, err
}
if err != nil {
return "", err
}
passAgain, err := askFn(ctx, fmt.Sprintf("Retype %s", name))
if err != nil {
return "", err
}
if pass == passAgain {
return pass, nil
}
out.Errorf(ctx, "Error: the entered password do not match")
}
return "", fmt.Errorf("no valid user input")
}