This repository was archived by the owner on Aug 6, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmodeler.go
More file actions
727 lines (707 loc) · 20.5 KB
/
Copy pathmodeler.go
File metadata and controls
727 lines (707 loc) · 20.5 KB
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package newt
import (
"bufio"
"bytes"
"fmt"
"io"
"sort"
"strconv"
"strings"
// Caltech Library Packages
"github.com/caltechlibrary/models"
)
// isSupportedElementType checks if the element type is supported by Newt, returns true if OK false is it is not
func isSupportedElementType(eType string) bool {
eType = strings.TrimSpace(strings.ToLower(eType))
for _, sType := range []string{"text", "textarea", "button", "checkbox", "radio", "date", "phone", "url", "email", "number", "integer", "select", "submit", "reset", "orcid" } {
if eType == sType {
return true
}
}
return false
}
// removeElement removes an element from a model
func removeElement(model *models.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
}
// removeModelRoutesAndTemplates removes the model and related routes and templates
func removeModelRoutesAndTemplates(ast *AST, in io.Reader, out io.Writer, eout io.Writer, modelId string) error {
// Step 1: Remove the model
modelFound := false
for i, model := range ast.Models {
if model.Id == modelId {
ast.Models = append(ast.Models[:i], ast.Models[(i+1):]...)
modelFound = true
}
}
if !modelFound {
return fmt.Errorf("failed to find %s", modelId)
}
for _, action := range []string{"create", "read", "update", "delete", "list"} {
id := mkName(modelId, action, "")
// Step 2: For each model remove the related routes
if err := ast.RemoveRouteById(id); err != nil {
fmt.Fprintf(eout, "%s\n", err)
} else {
ast.isChanged = true
}
// Step 3: For each model remove the related templates
if err := ast.RemoveTemplateById(id); err != nil {
fmt.Fprintf(eout, "%s\n", err)
} else {
ast.isChanged = true
}
}
return nil
}
// saveModelsRoutesAndTemplates takes a existing config structure,
// regenerates routes and templates if needed, writes the updated structure
// to disk
func saveModelsRoutesAndTemplates(configName string, ast *AST) error {
eBuf := bytes.NewBuffer([]byte{})
hasError := false
// Make sure that we have routes and template handling for each model
for _, model := range ast.Models {
if err := setupWebFormHandling(ast, model, "create"); err != nil {
fmt.Fprintf(eBuf, "%s\n", err)
hasError = true
}
if err := setupWebFormHandling(ast, model, "update"); err != nil {
fmt.Fprintf(eBuf, "%s\n", err)
hasError = true
}
if err := setupWebFormHandling(ast, model, "delete"); err != nil {
fmt.Fprintf(eBuf, "%s\n", err)
hasError = true
}
if err := setupReadHandling(ast, model, "read"); err != nil {
fmt.Fprintf(eBuf, "%s\n", err)
hasError = true
}
if err := setupReadHandling(ast, model, "list"); err != nil {
fmt.Fprintf(eBuf, "%s\n", err)
hasError = true
}
}
if err := ast.SaveAs(configName); err != nil {
fmt.Fprintf(eBuf, "%s\n", err)
hasError = true
}
if hasError {
return fmt.Errorf("%s", eBuf.Bytes())
}
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][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][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][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9A-Z]",
}
val := strings.TrimSpace(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 *models.Model, elementId string) ([]string, error) {
elementList := model.GetElementIds()
if !models.IsValidVarname(elementId) {
return elementList, fmt.Errorf("%q is not a valid elmeent id", elementId)
}
elem, err := models.NewElement(elementId)
if err != nil {
return elementList, err
}
model.Elements = append(model.Elements, elem)
elementList = model.GetElementIds()
return elementList, nil
}
// addModelStub adds an empty model to the ast.Models list. Returns
// a new model list and error value
func addModelStub(ast *AST, modelId string) ([]string, error) {
modelList := ast.GetModelIds()
if !models.IsValidVarname(modelId) {
return modelList, fmt.Errorf("%q is not a valid model id", modelId)
}
model, err := models.NewModel(modelId)
if err != nil {
return modelList, err
}
if err := ast.AddModel(model); err != nil {
return modelList, err
}
modelList = ast.GetModelIds()
sort.Strings(modelList)
// Setup templates and routes for model
return modelList, nil
}
// modifyModelTUI modify a specific model (e.g. add, modify remove model attributes)
func modifyModelTUI(ast *AST, in io.Reader, out io.Writer, eout io.Writer, modelId string) error {
buf := bufio.NewReader(in)
model, ok := ast.GetModelById(modelId)
if !ok {
return fmt.Errorf("cannot find %q", modelId)
}
for quit := false; !quit; {
elementList := model.GetElementIds()
//attributeList := model.GetAttributeIds()
menu, opt := selectMenuItem(in, out,
fmt.Sprintf("Manage %s model", modelId),
"Menu: model [d]escription, [e]lements or press enter when done",
[]string{
fmt.Sprintf("Model Id:\n\t\t%s\n", model.Id),
fmt.Sprintf("Description:\n\t\t%s\n", model.Description),
fmt.Sprintf("Element(s):\n\t\t%s\n", strings.Join(elementList, ",\n\t\t")),
}, false, "", "", true)
if len(menu) > 0 {
menu = menu[0:1]
}
switch menu {
case "d":
if opt == "" {
fmt.Fprintf(out, "\nEnter model description: ")
opt = getAnswer(buf, "", false)
}
if opt != "" {
model.Description = strings.TrimSpace(opt)
model.Changed(true)
}
case "e":
if err := modifyElementsTUI(in, out, eout, model); err != nil {
fmt.Fprintf(eout, "%s\n", err)
}
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "failed to underand %q\n", opt)
}
}
return nil
}
// modifyModelAttributesTUI provides a text UI for managing a model's attributes
func modifyModelAttributesTUI(model *models.Model, in io.Reader, out io.Writer, eout io.Writer) error {
buf := bufio.NewReader(in)
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 := selectMenuItem(in, out,
fmt.Sprintf("Manage %s attributes", model.Id), TuiStandardMenu,
attributeList, false, "", "", 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 = getAnswer(buf, "", true)
}
if !models.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 = getAnswer(buf, "", true)
}
fmt.Fprintf(out, "Enter %s's value: ", opt)
val := getAnswer(buf, "", false)
if val != "" {
model.Attributes[opt] = val
model.Changed(true)
}
case "r":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name to remove: ")
opt = getAnswer(buf, "", 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 *models.Model, in io.Reader, out io.Writer, eout io.Writer, elementId string) error {
buf := bufio.NewReader(in)
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))
}
menu, opt := selectMenuItem(in, out,
fmt.Sprintf("Manage %s.%s attributes", model.Id, elementId), TuiStandardMenu,
attributeList, true, "", "", true)
if len(menu) > 0 {
menu = menu[0:1]
}
opt, ok = getIdFromList(attributeList, opt)
switch menu {
case "a":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name: ")
opt = getAnswer(buf, "", true)
}
if !models.IsValidVarname(opt) {
fmt.Fprintf(eout, "%q is not a valid attribute name\n", opt)
} else {
elem.Attributes[opt] = ""
elem.Changed(true)
}
case "m":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name: ")
opt = getAnswer(buf, "", true)
}
fmt.Fprintf(out, "Enter %s's value: ", opt)
val := getAnswer(buf, "", false)
if val != "" {
elem.Attributes[opt] = val
elem.Changed(true)
}
case "r":
if opt == "" {
fmt.Fprintf(out, "Enter attribute name to remove: ")
opt = getAnswer(buf, "", 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 *models.Element, in io.Reader, out io.Writer, eout io.Writer, modelId string) error {
buf := bufio.NewReader(in)
if elem.Options == nil {
elem.Options = []map[string]string{}
}
for quit := false; !quit; {
optionsList := getValueLabelList(elem.Options)
menu, opt := selectMenuItem(in, out,
fmt.Sprintf("Manage %s.%s options", modelId, elem.Id),
"Menu [a]dd, [m]odify option no., [r]emove option no. or press enter when done",
optionsList,
true, "", "", 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 = getAnswer(buf, "", true)
} else {
val = strings.TrimSpace(opt)
}
if val == "" {
fmt.Fprintf(eout, "Error, an option value required\n")
} else {
fmt.Fprintf(out, "Enter an option label: ")
label = getAnswer(buf, "", 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 = getDigit(buf, optionsList)
}
if ok {
option := elem.Options[pos]
val, label, _ := getValAndLabel(option)
fmt.Fprintf(out, "Enter an option label (for %q): ", val)
answer := getAnswer(buf, "", 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 = getDigit(buf, 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 *models.Model, in io.Reader, out io.Writer, eout io.Writer, elementId string) error {
buf := bufio.NewReader(in)
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 = selectMenuItem(in, out,
fmt.Sprintf("Manage %s.%s element", model.Id, elementId),
"Menu [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 %s", strings.Join(attributeList, ",\n\t\t")),
fmt.Sprintf("options %s", strings.Join(optionsList, ",\n\t\t")),
},
false, "", "", true)
case "textarea":
menu, opt = selectMenuItem(in, out,
fmt.Sprintf("Manage %s.%s element", model.Id, elementId),
"Menu [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 %s", strings.Join(attributeList, ",\n\t\t")),
},
false, "", "", true)
default:
menu, opt = selectMenuItem(in, out,
fmt.Sprintf("Manage %s.%s element", model.Id, elementId),
"Menu [t]ype, [l]abel, [o]bject identifier, [p]attern, [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("pattern %s", elem.Pattern),
fmt.Sprintf("attributes %s", strings.Join(attributeList, ",\n\t\t")),
fmt.Sprintf("object identifier? %t", elem.IsObjectId),
},
false, "", "", 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 = getAnswer(buf, "", false)
}
if opt != "" {
eType := strings.TrimSpace(opt)
if ok := 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 = getAnswer(buf, "", 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 = strings.TrimSpace(opt)
}
model.Changed(true)
}
case "l":
if opt == "" {
fmt.Fprintf(out, "Enter a label: ")
opt = getAnswer(buf, "", false)
}
if opt != "" {
// NOTE: remove a pattern by having it match everything, i.e. astrix
if opt != elem.Label {
elem.Label = strings.TrimSpace(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 "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "did not understand %q\n", menu)
}
}
return nil
}
func removeElementFromModel(model *models.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(in io.Reader, out io.Writer, eout io.Writer, model *models.Model) error {
var (
err error
answer string
)
buf := bufio.NewReader(in)
// 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 := selectMenuItem(in, out,
fmt.Sprintf("Manage %s elements", model.Id), TuiStandardMenu,
elementList, true, "", "", 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 = getAnswer(buf, "", 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 = getAnswer(buf, "", 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 = getAnswer(buf, "", 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
}
// modelerTUI takes configuration and then runs the interactive text user interface modeler.
func modelerTUI(ast *AST, in io.Reader, out io.Writer, eout io.Writer, configName string, newModelIds []string) error {
var (
err error
answer string
)
buf := bufio.NewReader(in)
if ast.Models == nil {
ast.Models = []*models.Model{}
}
modelList := ast.GetModelIds()
sort.Strings(modelList)
if len(newModelIds) > 0 {
for _, modelId := range newModelIds {
modelList, err = addModelStub(ast, modelId)
if err != nil {
fmt.Fprintf(eout, "WARNING: %s\n", err)
}
}
}
for quit := false; !quit; {
menu, opt := selectMenuItem(in, out,
"Manage Models",
TuiStandardMenu,
modelList, true, "", "", true)
if len(menu) > 1 {
menu = menu[0:1]
}
modelId, ok := getIdFromList(modelList, opt)
switch menu {
case "a":
if !ok {
fmt.Fprintf(out, "Enter model id to add: ")
opt = getAnswer(buf, "", false)
modelId, ok = getIdFromList(modelList, opt)
}
if ok {
modelList, err = addModelStub(ast, modelId)
if err != nil {
fmt.Fprintf(eout, "WARNING: %s\n", err)
}
}
case "m":
if !ok {
fmt.Fprintf(out, "Enter model id to modify: ")
opt = getAnswer(buf, "", false)
modelId, ok = getIdFromList(modelList, opt)
}
if err := modifyModelTUI(ast, in, out, eout, modelId); err != nil {
fmt.Fprintf(eout, "ERROR (%q): %s\n", modelId, err)
}
case "r":
if !ok {
fmt.Fprintf(out, "Enter model id to remove: ")
opt = getAnswer(buf, "", false)
modelId, ok = getIdFromList(modelList, opt)
}
if err := removeModelRoutesAndTemplates(ast, in, out, eout, modelId); err != nil {
fmt.Fprintf(eout, "ERROR (%q): %s\n", modelId, err)
}
modelList = ast.GetModelIds()
case "q":
quit = true
case "":
quit = true
default:
fmt.Fprintf(eout, "\n\nERROR: Did not understand %q\n\n", answer)
}
}
if ast.HasChanges() {
fmt.Fprintf(out, "Save before exiting (Y/n)? ")
answer = getAnswer(buf, "y", true)
if answer == "y" {
if err := saveModelsRoutesAndTemplates(configName, ast); err != nil {
return err
}
}
}
return nil
}