-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.go
657 lines (635 loc) · 17.5 KB
/
interactive.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package models
import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
// 3rd Party Packages
"gopkg.in/yaml.v3"
)
// ModelToYAML renders a Model struct as YAML
func ModelToYAML(out io.Writer, model *Model) error {
encoder := yaml.NewEncoder(out)
encoder.SetIndent(2)
if err := encoder.Encode(model); err != nil {
return err
}
return nil
}
// removeElement removes an element from a model
func removeElement(model *Model, in io.Reader, out io.Writer, eout io.Writer, elementId string) error {
elemFound := false
for i, elem := range model.Elements {
if elem.Id == elementId {
model.Elements = append(model.Elements[:i], model.Elements[(i+1):]...)
model.Changed(true)
elemFound = true
}
}
if !elemFound {
return fmt.Errorf("failed to find %s.%s", model.Id, elementId)
}
return nil
}
// normalizeInputType takes a string and returns a normlized input type
// (e.g. lowercased and spaces trim) and if the type is an alias
// then a pattern is returned along with the normalized type.
// e.g. DATE -> date,""
//
// eMail -> email, ""
// ORCID -> orcid, "[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9A-Z]"
func normalizeInputType(inputType string) (string, string) {
patternMap := map[string]string{
// This is where we put the aliases to common regexp validated patterns
"orcid": "[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9A-Z]",
}
val := strings.ToLower(inputType)
if pattern, ok := patternMap[val]; ok {
return val, pattern
}
return val, ""
}
// addElementStub adds an empty element to model.Elements list. Returns a new model list and error value
func addElementStub(model *Model, elementId string) ([]string, error) {
elementList := model.GetElementIds()
if !IsValidVarname(elementId) {
return elementList, fmt.Errorf("%q is not a valid elmeent id", elementId)
}
elem, err := NewElement(elementId)
if err != nil {
return elementList, err
}
model.Elements = append(model.Elements, elem)
elementList = model.GetElementIds()
return elementList, nil
}
// modifyModelAttributesTUI provides a text UI for managing a model's attributes
func modifyModelAttributesTUI(model *Model, in io.Reader, out io.Writer, eout io.Writer) error {
prompt := NewPrompt(in, out, eout)
if model.Attributes == nil {
model.Attributes = map[string]string{}
}
for quit := false; !quit; {
attributeList := []string{}
for k, v := range model.Attributes {
attributeList = append(attributeList, fmt.Sprintf("%s -> %q", k, v))
}
menu, opt := prompt.SelectMenu(
fmt.Sprintf("Manage %s attributes (none required)", model.Id),
"Choices [a]dd, [m]odify, [r]emove or press enter when done",
attributeList, "", "", true)
if len(menu) > 0 {
menu = menu[0:1]
}
var ok bool
opt, ok = getIdFromList(attributeList, opt)
switch menu {
case "a":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name: ")
opt = prompt.GetAnswer("", true)
}
if !IsValidVarname(opt) {
fmt.Fprintf(eout, "%q is not a valid attribute name\n", opt)
} else {
model.Attributes[opt] = ""
model.Changed(true)
}
case "m":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name: ")
opt = prompt.GetAnswer("", true)
}
fmt.Fprintf(out, "Enter %s's value: ", opt)
val := prompt.GetAnswer("", false)
if val != "" {
model.Attributes[opt] = val
model.Changed(true)
}
case "r":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name to remove: ")
opt = prompt.GetAnswer("", true)
opt, ok = getIdFromList(attributeList, opt)
}
if ok {
if _, ok := model.Attributes[opt]; ok {
delete(model.Attributes, opt)
model.Changed(true)
}
}
if !ok {
fmt.Fprintf(eout, "failed to find %q in attributes\n", opt)
}
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "failed to underand %q\n", opt)
}
}
return nil
}
// modifyElementAttributesTUI provides a text UI for managing a model's element's attributes
func modifyElementAttributesTUI(model *Model, in io.Reader, out io.Writer, eout io.Writer, elementId string) error {
prompt := NewPrompt(in, out, eout)
elem, _ := model.GetElementById(elementId)
for quit := false; !quit; {
var ok bool
attributeList := []string{}
for k, v := range elem.Attributes {
attributeList = append(attributeList, fmt.Sprintf("%s -> %q", k, v))
}
val := ""
menu, opt := prompt.SelectMenu(
fmt.Sprintf("Modify element %s.%s attributes", model.Id, elementId),
"Choices [a]dd, [m]odify, [r]emove or press enter when done",
attributeList, "", "", true)
if len(menu) > 0 {
menu = menu[0:1]
}
if i := strings.Index(opt, " "); i >= 0 {
opt, val = opt[0:i], opt[i+1:]
}
opt, ok = getIdFromList(attributeList, opt)
switch menu {
case "a":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name: ")
opt = prompt.GetAnswer("", true)
}
if !IsValidVarname(opt) {
fmt.Fprintf(eout, "%q is not a valid attribute name\n", opt)
} else {
elem.Attributes[opt] = val
elem.Changed(true)
}
case "m":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name: ")
opt = prompt.GetAnswer("", true)
}
if val == "" {
fmt.Fprintf(out, "Enter %s's value: ", opt)
val = prompt.GetAnswer("", false)
}
if val != "" {
elem.Attributes[opt] = val
elem.Changed(true)
}
case "r":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name to remove: ")
opt = prompt.GetAnswer("", true)
opt, ok = getIdFromList(attributeList, opt)
}
if ok {
if _, ok = elem.Attributes[opt]; ok {
delete(elem.Attributes, opt)
elem.Changed(true)
}
}
if !ok {
fmt.Fprintf(eout, "failed to find %q in attributes\n", opt)
}
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "failed to underand %q\n", opt)
}
}
return nil
}
func modifySelectElementTUI(elem *Element, in io.Reader, out io.Writer, eout io.Writer, modelId string) error {
prompt := NewPrompt(in, out, eout)
if elem.Options == nil {
elem.Options = []map[string]string{}
}
for quit := false; !quit; {
optionsList := getValueLabelList(elem.Options)
menu, opt := prompt.SelectMenu(
fmt.Sprintf("Manage %s.%s options", modelId, elem.Id),
"Choices [a]dd, [m]odify, [r]emove or press enter when done",
optionsList,
"", "", true)
if len(menu) > 0 {
menu = menu[0:1]
}
var (
val string
label string
)
switch menu {
case "a":
if opt == "" {
fmt.Fprintf(out, "Enter an option value: ")
val = prompt.GetAnswer("", true)
} else {
val = opt
}
if val == "" {
fmt.Fprintf(eout, "Error, an option value required\n")
} else {
fmt.Fprintf(out, "Enter an option label: ")
label = prompt.GetAnswer("", false)
if label == "" {
label = val
}
option := map[string]string{
val: label,
}
elem.Options = append(elem.Options, option)
elem.Changed(true)
}
case "m":
pos, ok := -1, false
if i, err := strconv.Atoi(opt); err == nil {
// Adjust to a zero based array
if i > 0 && i <= len(optionsList) {
pos, ok = (i - 1), true
}
}
if !ok {
fmt.Fprintf(out, "Enter option number: ")
pos, ok = prompt.GetDigit(optionsList)
}
if ok {
option := elem.Options[pos]
val, label, _ := getValAndLabel(option)
fmt.Fprintf(out, "Enter an option label (for %q): ", val)
answer := prompt.GetAnswer("", false)
if answer != "" {
label = answer
}
if label == "" {
label = val
}
option = map[string]string{
val: label,
}
// Replace the option in the options list.
elem.Options[pos] = option
elem.Changed(true)
} else {
fmt.Fprintf(eout, "Failed to identify option %q\n", opt)
}
case "r":
pos, ok := -1, false
if i, err := strconv.Atoi(opt); err == nil {
// Adjust to a zero based array
if i > 0 && i <= len(optionsList) {
pos, ok = (i - 1), true
}
} else {
fmt.Fprintf(out, "Enter option number: ")
pos, ok = prompt.GetDigit(optionsList)
}
if ok {
elem.Options = append(elem.Options[0:pos], elem.Options[(pos+1):]...)
elem.Changed(true)
} else {
fmt.Fprintf(eout, "failed to remove option number (%d) %q\n", pos, opt)
}
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "did not understand, %s %s\n", menu, opt)
}
}
return nil
}
func modifyElementTUI(model *Model, in io.Reader, out io.Writer, eout io.Writer, elementId string) error {
prompt := NewPrompt(in, out, eout)
elem, ok := model.GetElementById(elementId)
if !ok {
return fmt.Errorf("could not find %q element", elementId)
}
var (
menu string
opt string
)
for quit := false; !quit; {
attributeList := getAttributeIds(elem.Attributes)
switch elem.Type {
case "select":
optionsList := getValueLabelList(elem.Options)
menu, opt = prompt.SelectMenu(
fmt.Sprintf("Modify element %s.%s", model.Id, elementId),
"Choices [t]ype, [l]abel, [a]ttributes, [o]ptions or press enter when done",
[]string{
fmt.Sprintf("id %s", elementId),
fmt.Sprintf("type %s", elem.Type),
fmt.Sprintf("label %s", elem.Label),
fmt.Sprintf("attributes:\n\t\t%s", strings.Join(attributeList, ",\n\t\t")),
fmt.Sprintf("options:\n\t\t%s", strings.Join(optionsList, ",\n\t\t")),
},
"", "", true)
case "textarea":
menu, opt = prompt.SelectMenu(
fmt.Sprintf("Modify element %s.%s", model.Id, elementId),
"Choices [t]ype, [l]abel, [a]ttributes or press enter when done",
[]string{
fmt.Sprintf("id %s", elementId),
fmt.Sprintf("type %s", elem.Type),
fmt.Sprintf("label %s", elem.Label),
fmt.Sprintf("attributes:\n\t\t%s", strings.Join(attributeList, ",\n\t\t")),
},
"", "", true)
default:
menu, opt = prompt.SelectMenu(
fmt.Sprintf("Manage %s.%s element", model.Id, elementId),
"Choices [t]ype, [l]abel, [o]bject identifier, [p]attern, [a]ttributes, [g]enerator or press enter when done",
[]string{
fmt.Sprintf("id %s", elementId),
fmt.Sprintf("type %s", elem.Type),
fmt.Sprintf("label %s", elem.Label),
fmt.Sprintf("pattern %s", elem.Pattern),
fmt.Sprintf("attributes:\n\t\t%s", strings.Join(attributeList, ",\n\t\t")),
fmt.Sprintf("object identifier? %t", elem.IsObjectId),
fmt.Sprintf("generator %s", elem.Generator),
},
"", "", true)
}
if len(menu) > 0 {
menu = menu[0:1]
}
switch menu {
case "t":
if opt == "" {
fmt.Fprintf(out, `Enter type string (e.g. text, email, date, textarea, select, orcid): `)
opt = prompt.GetAnswer("", false)
}
if opt != "" {
eType := opt
if ok := model.IsSupportedElementType(eType); !ok {
fmt.Fprintf(eout, "%q is not a supported element type", opt)
} else {
// FIXME: If elem.Type is select I need to provide an choices TUI for values and labels
elem.Type, elem.Pattern = normalizeInputType(eType)
elem.Changed(true)
if elem.Type == "select" {
if err := modifySelectElementTUI(elem, in, out, eout, model.Id); err != nil {
fmt.Fprintf(eout, "ERROR (%q): %s\n", elementId, err)
}
}
}
}
case "p":
if opt == "" {
fmt.Fprintf(out, "Enter the regexp to use: ")
opt = prompt.GetAnswer("", false)
}
// FIXME: how do I clear a pattern if I nolonger want to use one?
if opt != "" {
// NOTE: remove a pattern by having it match everything, i.e. astrix
if opt == "*" {
elem.Pattern = ""
} else {
elem.Pattern = opt
}
model.Changed(true)
}
case "l":
if opt == "" {
fmt.Fprintf(out, "Enter a label: ")
opt = prompt.GetAnswer("", false)
}
if opt != "" {
// NOTE: remove a pattern by having it match everything, i.e. astrix
if opt != elem.Label {
elem.Label = opt
}
model.Changed(true)
}
case "a":
if err := modifyElementAttributesTUI(model, in, out, eout, elementId); err != nil {
fmt.Fprintf(eout, "%s\n", err)
}
case "o":
if elem.Type == "select" {
if err := modifySelectElementTUI(elem, in, out, eout, model.Id); err != nil {
fmt.Fprintf(eout, "ERROR (%q): %s\n", elem.Id, err)
}
} else {
elem.IsObjectId = !elem.IsObjectId
elem.Changed(true)
}
case "g":
if opt == "" {
fmt.Fprintf(out, "Enter generator (e.g. autoincrement, uuid, current_timestamp, created_timestamp, current_date, created_date) ")
opt = prompt.GetAnswer("", true)
}
fmt.Fprintf(out, "DEBUG opt -> %q, elem.Generator -> %q\n", opt, elem.Generator)
if opt != "" {
if strings.HasPrefix(opt, "\"") || strings.HasPrefix(opt, "'") {
elem.Generator = ""
fmt.Fprintf(out, "DEBUG opt -> %q, elem.Generator -> %q\n", opt, elem.Generator)
elem.Changed(true)
} else if opt != elem.Generator {
elem.Generator = opt
elem.Changed(true)
}
}
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "did not understand %q\n", menu)
}
}
return nil
}
func removeElementFromModel(model *Model, elementId string) error {
for i, elem := range model.Elements {
if elem.Id == elementId {
model.Elements = append(model.Elements[0:i], model.Elements[(i+1):]...)
model.Changed(true)
return nil
}
}
return fmt.Errorf("%s not found", elementId)
}
// modifyElementsTUI modify a specific model's element list.
func modifyElementsTUI(model *Model, in io.Reader, out io.Writer, eout io.Writer) error {
var (
err error
answer string
)
prompt := NewPrompt(in, out, eout)
// FIXME: Need to support editing model attributes, then allow for modifying model's body to be modified.
for quit := false; !quit; {
elementList := model.GetElementIds()
menu, opt := prompt.SelectMenu(
fmt.Sprintf("Manage %s elements", model.Id),
"Choices [a]dd, [m]odify, [r]emove or press enter when done",
elementList,
"", "", true)
if len(menu) > 1 {
menu = menu[0:1]
}
elementId, ok := getIdFromList(elementList, opt)
switch menu {
case "a":
if !ok {
fmt.Fprintf(out, "Enter element id to add: ")
opt = prompt.GetAnswer("", false)
elementId, ok = getIdFromList(elementList, opt)
}
if ok {
elementList, err = addElementStub(model, elementId)
if err != nil {
fmt.Fprintf(eout, "WARNING: %s\n", err)
}
}
case "m":
if elementId == "" {
fmt.Fprintf(out, "Enter element id to modify: ")
opt = prompt.GetAnswer("", false)
elementId, ok = getIdFromList(elementList, opt)
}
if err := modifyElementTUI(model, in, out, eout, elementId); err != nil {
fmt.Fprintf(eout, "ERROR (%q): %s\n", elementId, err)
}
case "r":
if elementId == "" {
fmt.Fprintf(out, "Enter element id to remove: ")
opt = prompt.GetAnswer("", false)
elementId, ok = getIdFromList(elementList, opt)
}
if ok {
if err := removeElementFromModel(model, elementId); err != nil {
fmt.Fprintf(eout, "ERROR (%q): %s\n", elementId, err)
}
}
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "\n\nERROR: Did not understand %q\n\n", answer)
}
}
return nil
}
func modifyModelMetadataTUI(model *Model, in io.Reader, out io.Writer, eout io.Writer) error {
prompt := NewPrompt(in, out, eout)
for quit := false; !quit; {
menu, opt := prompt.SelectMenu(
"Manage Model Metadata",
"Menu [i]d, [d]escription or press enter when done",
[]string{
fmt.Sprintf("id: %q", model.Id),
//fmt.Sprintf("title: %q", model.Title),
fmt.Sprintf("description: %q", model.Description),
},
"", "", true)
if len(menu) > 0 {
menu = menu[0:1]
}
switch menu {
case "i":
if opt == "" {
fmt.Fprintf(out, `Enter id: `)
opt = prompt.GetAnswer("", false)
}
if opt != "" {
if opt != model.Id {
model.Id = opt
model.Changed(true)
}
}
case "d":
if opt == "" {
fmt.Fprintf(out, `Enter Description: `)
opt = prompt.GetAnswer("", false)
}
if opt != "" {
if opt != model.Description {
model.Description = opt
model.Changed(true)
}
}
case "q":
quit = true
default:
quit = true
}
}
return nil
}
func ModelInteractively(model *Model) error {
in := os.Stdin
out := os.Stdout
eout := os.Stderr
// Manage Model Metadata
if err := modifyModelMetadataTUI(model, in, out, eout); err != nil {
return err
}
/*
// Manage Model Attributes
if err := modifyModelAttributesTUI(model, in, out, eout); err != nil {
return err
}
*/
// Manage Model Elements
if err := modifyElementsTUI(model, in, out, eout); err != nil {
return err
}
return nil
}
//
// Misc functions
//
func getIdFromList(list []string, id string) (string, bool) {
nRe := regexp.MustCompile(`^[0-9]+$`)
// See if we have been given a model number or a name
if isDigit := nRe.Match([]byte(id)); isDigit {
mNo, err := strconv.Atoi(id)
if err == nil {
// Adjust provided integer for zero based index.
if mNo > 0 {
mNo--
} else {
mNo = 0
}
if mNo < len(list) {
if strings.Contains(list[mNo], " ") {
parts := strings.SplitN(list[mNo], " ", 2)
return parts[0], true
}
return list[mNo], true
}
}
}
if IsValidVarname(id) {
return id, true
}
return "", false
}
// Get return the first key and value pair
func getValAndLabel(option map[string]string) (string, string, bool) {
for val, label := range option {
return val, label, true
}
return "", "", false
}
// getValueLabelList takes an array of map[string]string and yours a list of
// strings indicating the value and label
func getValueLabelList(list []map[string]string) []string {
options := []string{}
for _, m := range list {
val, label, ok := getValAndLabel(m)
if ok {
options = append(options, fmt.Sprintf("%s %s", val, label))
}
}
return options
}