forked from ktr0731/evans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_inputter.go
320 lines (274 loc) · 7.52 KB
/
prompt_inputter.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package gateway
import (
"strings"
"github.com/AlecAivazis/survey"
prompt "github.com/c-bata/go-prompt"
"github.com/golang/protobuf/proto"
"github.com/ktr0731/evans/adapter/protobuf"
"github.com/ktr0731/evans/config"
"github.com/ktr0731/evans/entity"
shellstring "github.com/ktr0731/go-shellstring"
"github.com/pkg/errors"
)
var (
ErrUnknownOneofFieldName = errors.New("unknown oneof field name")
ErrUnknownEnumName = errors.New("unknown enum name")
EORF = errors.New("end of repeated field")
)
// for mocking
type prompter interface {
Input() string
Select(msg string, opts []string) (string, error)
SetPrefix(prefix string)
SetPrefixColor(color prompt.Color) error
}
type RealPrompter struct {
fieldPrompter *prompt.Prompt
currentPrefix string
}
func newRealPrompter() *RealPrompter {
executor := func(in string) {
return
}
completer := func(d prompt.Document) []prompt.Suggest {
return nil
}
p := &RealPrompter{}
p.fieldPrompter = prompt.New(executor, completer, prompt.OptionLivePrefix(p.livePrefix))
return p
}
func (p *RealPrompter) Input() string {
return p.fieldPrompter.Input()
}
func (p *RealPrompter) Select(msg string, opts []string) (string, error) {
var choice string
err := survey.AskOne(&survey.Select{
Message: msg,
Options: opts,
}, &choice, nil)
return choice, err
}
func (p *RealPrompter) SetPrefix(prefix string) {
p.currentPrefix = prefix
}
func (p *RealPrompter) SetPrefixColor(color prompt.Color) error {
return prompt.OptionPrefixTextColor(color)(p.fieldPrompter)
}
func (p *RealPrompter) livePrefix() (string, bool) {
return p.currentPrefix, true
}
// mixin go-prompt
func NewPrompt(config *config.Config, env entity.Environment) *Prompt {
return newPrompt(newRealPrompter(), config, env)
}
type Prompt struct {
prompt prompter
config *config.Config
env entity.Environment
}
func newPrompt(prompt prompter, config *config.Config, env entity.Environment) *Prompt {
return &Prompt{
prompt: prompt,
config: config,
env: env,
}
}
// impl of port.Inputter
func (i *Prompt) Input(reqType entity.Message) (proto.Message, error) {
setter := protobuf.NewMessageSetter(reqType)
fields := reqType.Fields()
// DarkGreen is the initial color
return newFieldInputter(i.prompt, i.config.Input.PromptFormat, setter, []string{}, false, prompt.DarkGreen).Input(fields)
}
// fieldInputter inputs each fields of req in interactively
// first fieldInputter is instantiated per one request
type fieldInputter struct {
prompt prompter
setter *protobuf.MessageSetter
prefixFormat string
ancestor []string
color prompt.Color
// enteredEmptyInput is used to terminate repeated field inputting
// if input is empty and enteredEmptyInput is true, exit repeated input prompt
enteredEmptyInput bool
// the field has parent field (in other words, the field is child of a message field)
hasAncestorAndHasRepeatedField bool
}
func newFieldInputter(
prompter prompter,
prefixFormat string,
setter *protobuf.MessageSetter,
ancestor []string,
hasAncestorAndHasRepeatedField bool,
color prompt.Color,
) *fieldInputter {
return &fieldInputter{
prompt: prompter,
setter: setter,
prefixFormat: prefixFormat,
ancestor: ancestor,
color: color,
hasAncestorAndHasRepeatedField: hasAncestorAndHasRepeatedField,
}
}
// Input will call itself for nested messages
//
// e.g.
// message Foo {
// Bar bar = 1;
// string baz = 2;
// }
//
// Input is called two times
// one for Foo's primitive fields
// another one for bar's primitive fields
func (i *fieldInputter) Input(fields []entity.Field) (proto.Message, error) {
if err := i.prompt.SetPrefixColor(i.color); err != nil {
return nil, err
}
for _, field := range fields {
if field.IsRepeated() {
for {
if err := i.inputField(field); err == EORF {
break
} else if err != nil {
return nil, err
}
}
i.enteredEmptyInput = false
} else {
// if oneof, choose one from selection
if oneof, ok := field.(entity.OneOfField); ok {
var err error
field, err = i.chooseOneof(oneof)
if err != nil {
return nil, err
}
}
if err := i.inputField(field); err != nil {
return nil, err
}
}
}
return i.setter.Done(), nil
}
func (i *fieldInputter) chooseOneof(oneof entity.OneOfField) (entity.Field, error) {
options := make([]string, 0, len(oneof.Choices()))
fieldOf := map[string]entity.Field{}
for _, choice := range oneof.Choices() {
options = append(options, choice.FieldName())
fieldOf[choice.FieldName()] = choice
}
choice, err := i.prompt.Select(oneof.FieldName(), options)
if err != nil {
return nil, err
}
f, ok := fieldOf[choice]
if !ok {
return nil, errors.Wrap(ErrUnknownOneofFieldName, choice)
}
return f, nil
}
func (i *fieldInputter) chooseEnum(enum entity.EnumField) (entity.EnumValue, error) {
options := make([]string, 0, len(enum.Values()))
valOf := map[string]entity.EnumValue{}
for _, v := range enum.Values() {
options = append(options, v.Name())
valOf[v.Name()] = v
}
choice, err := i.prompt.Select(enum.Name(), options)
if err != nil {
return nil, err
}
c, ok := valOf[choice]
if !ok {
return nil, errors.Wrap(ErrUnknownEnumName, choice)
}
return c, nil
}
func (i *fieldInputter) inputField(field entity.Field) error {
switch f := field.(type) {
case entity.EnumField:
v, err := i.chooseEnum(f)
if err != nil {
return err
}
if err := i.setter.SetField(f, v.Number()); err != nil {
return err
}
case entity.MessageField:
setter := protobuf.NewMessageSetter(f)
fields := f.Fields()
msg, err := newFieldInputter(i.prompt, i.prefixFormat, setter, append(i.ancestor, f.FieldName()), i.hasAncestorAndHasRepeatedField || f.IsRepeated(), i.color).Input(fields)
if err != nil {
return err
}
if err := i.setter.SetField(f, msg); err != nil {
return err
}
// increment prompt color to next one
i.color = (i.color + 1) % 16
case entity.PrimitiveField:
i.prompt.SetPrefix(i.makePrefix(field))
v, err := i.inputPrimitiveField(f)
if err != nil {
return err
}
if err := i.setter.SetField(f, v); err != nil {
return err
}
default:
panic("unknown type: " + field.PBType())
}
return nil
}
func (i *fieldInputter) inputPrimitiveField(f entity.PrimitiveField) (interface{}, error) {
l := i.prompt.Input()
part, err := shellstring.Parse(l)
if err != nil {
return "", err
}
if len(part) > 1 {
return nil, errors.New("invalid input string")
}
var in string
if len(part) != 0 {
in = part[0]
}
if in == "" {
// if f is repeated or
// ancestor has repeated field
if f.IsRepeated() || i.hasAncestorAndHasRepeatedField {
if i.enteredEmptyInput {
return nil, EORF
}
i.enteredEmptyInput = true
// ignore the input
return i.inputPrimitiveField(f)
}
} else {
i.enteredEmptyInput = false
}
return protobuf.ConvertValue(in, f)
}
// makePrefix makes prefix for field f.
func (i *fieldInputter) makePrefix(f entity.PrimitiveField) string {
return makePrefix(i.prefixFormat, f, i.ancestor, i.hasAncestorAndHasRepeatedField)
}
const (
repeatedStr = "<repeated> "
ancestorDelimiter = "::"
)
func makePrefix(s string, f entity.PrimitiveField, ancestor []string, ancestorHasRepeated bool) string {
joinedAncestor := strings.Join(ancestor, ancestorDelimiter)
if joinedAncestor != "" {
joinedAncestor += ancestorDelimiter
}
s = strings.Replace(s, "{ancestor}", joinedAncestor, -1)
s = strings.Replace(s, "{name}", f.FieldName(), -1)
s = strings.Replace(s, "{type}", f.PBType(), -1)
if f.IsRepeated() || ancestorHasRepeated {
return repeatedStr + s
}
return s
}