/
service.go
238 lines (206 loc) · 4.61 KB
/
service.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package ask
import (
"bufio"
"encoding"
"errors"
"fmt"
"io"
"os"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
// Service is the handler to ask values.
type Service struct {
Prototype
}
var static Service
// envar gets value from environment variable
func (s *Service) envar() string {
return os.Getenv(s.Prototype.Envar)
}
// defaultValue gets default value from the prototype and environment variable
func (s *Service) defaultValue() string {
def := s.Prototype.Default
if env := s.envar(); env != "" {
def = env
}
return def
}
// prompt makes text to mean that the program is waiting for user to type in some answer.
// A message should take form a question.
func (s *Service) prompt() string {
p := s.Prototype
prompt := p.Message
def := s.defaultValue()
if def != "" {
if p.Hidden {
def = "*****"
}
prompt += " [" + def + "]"
}
if p.Column > 0 {
prompt = fmt.Sprintf(fmt.Sprintf("%%%ds", p.Column), prompt)
}
return prompt + " : "
}
func (s *Service) reader() io.Reader {
if s.Prototype.Reader != nil {
return s.Prototype.Reader
}
return os.Stdin
}
func (s *Service) writer() io.Writer {
if s.Prototype.Writer != nil {
return s.Prototype.Writer
}
return os.Stdout
}
func (s *Service) isOptional(input string) error {
if input != "" {
return nil
}
if s.Prototype.Optional {
return ErrSkip
}
return errors.New("it must not be empty")
}
func (s *Service) isInEnum(input string) error {
enum := s.Prototype.Enum
for _, e := range enum {
if e == input {
return nil
}
}
var hint string
switch len(enum) {
case 0:
return nil
case 1:
hint = enum[0]
case 2:
hint = strings.Join(enum, " OR ")
default:
hint = strings.Join(enum[0:len(enum)-1], ", ") + " OR " + enum[len(enum)-1]
}
return fmt.Errorf("it must be %s", hint)
}
func (s *Service) isMatched(input string) error {
matcher := s.Prototype.Matcher
if matcher == nil {
return nil
}
if matcher.MatchString(input) {
return nil
}
return fmt.Errorf("it must be matched for %s", s.Prototype.Matcher.String())
}
func (s *Service) isValid(input string) error {
validator := s.Prototype.Validation
if validator == nil {
return nil
}
if err := validator(input); err != nil {
return err
}
return nil
}
func (s *Service) getInput() (string, error) {
if s.Prototype.Hidden && s.Prototype.Reader == nil {
buf, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprintln(s.writer())
return string(buf), err
}
scanner := bufio.NewScanner(s.reader())
if scanner.Scan() {
return scanner.Text(), nil
}
return "", io.EOF
}
// Doer will do.
type Doer interface {
Do() error
}
type doFunc func() error
func (d doFunc) Do() error { return d() }
// ErrSkip will skip to ask a value.
var ErrSkip = errors.New("skip")
// Var sets encoding.TextUnmarshaler.
func (s *Service) Var(parse encoding.TextUnmarshaler) Doer {
return doFunc(func() error {
return s.Ask(parse)
})
}
type UnmarshalFunc func([]byte) error
func (f UnmarshalFunc) UnmarshalText(raw []byte) error {
return f(raw)
}
type ParseFunc func(string) error
func (f ParseFunc) UnmarshalText(raw []byte) error {
return f(string(raw))
}
// AskFunc will get a value from input and pass it to unmarshal func.
func (s *Service) AskUnmarshalFunc(unmarshal UnmarshalFunc) error {
return s.Ask(unmarshal)
}
// AskParseFunc will get a value from input and pass it to parser func.
func (s *Service) AskParseFunc(parse ParseFunc) error {
return s.Ask(parse)
}
// Ask will get a value from input and pass it to encoding.TextUnmarshaler.
func (s *Service) Ask(unmarshaler encoding.TextUnmarshaler) error {
if bef := s.Prototype.Before; bef != nil {
switch err := bef(); err {
case nil:
// continue
default:
return err
}
}
for i := 0; s.Prototype.Limit < 1 || i < s.Prototype.Limit; i++ {
if bef := s.Prototype.BeforeEach; bef != nil {
switch err := bef(i); err {
case nil:
// continue
default:
return err
}
}
switch err := s.askOnce(unmarshaler); err {
case ErrSkip:
return nil
case nil:
return nil
default:
fmt.Fprintln(s.writer(), err.Error())
}
}
return errors.New("asked over the limit")
}
func (s *Service) askOnce(unmarshaler encoding.TextUnmarshaler) error {
prompt := s.prompt()
fmt.Fprint(s.writer(), prompt)
input, err := s.getInput()
if err != nil {
return err
}
if input == "" {
if def := s.defaultValue(); def != "" {
input = def
}
}
for _, p := range []ParseFunc{
s.isOptional,
s.isInEnum,
s.isMatched,
s.isValid,
func(s string) error { return unmarshaler.UnmarshalText([]byte(s)) },
} {
switch err := p(input); err {
case nil:
// continue
default:
return err
}
}
return nil
}