This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
/
prompts.go
844 lines (722 loc) · 21.6 KB
/
prompts.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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
)
func quit() {
stopContainer()
os.Exit(0)
}
func selectSetup() error {
items := []string{"Use Docker on your PC", "Help", "Quit"}
index := 1
if !cfg.DisableKubernetes {
items = append(items[:index+1], items[index:]...)
items[index] = "Use a Kubernetes Cluster"
index++
}
if !cfg.DisableIG {
items = append(items[:index+1], items[index:]...)
items[index] = "Install FHIR package"
}
prompt := promptui.Select{
Label: "Please choose how you want to run Instant. \nChoose Docker if you're running on your PC. \nIf you want to run Instant on Kubernetes, then you have should been provided credentials or have Kubernetes running on your PC.",
Items: items,
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectSetup() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "Use Docker on your PC":
err = debugDocker()
if err != nil {
return err
}
err = selectDefaultOrCustom()
case "Use a Kubernetes Cluster":
err = debugKubernetes()
if err != nil {
return err
}
err = selectPackageCluster()
case "Install FHIR package":
err = selectUtil()
case "Help":
fmt.Println(getHelpText(true, ""))
selectSetup()
case "Quit":
quit()
}
return err
}
func selectUtil() error {
fmt.Println("Enter URL for the published package")
// prompt for url
prompt := promptui.Prompt{
Label: "URL",
}
ig_url, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectUtil() prompt failed")
}
fhir_server, params, err := selectFHIR()
if err != nil {
return err
}
fmt.Println("FHIR Server target:", fhir_server)
err = loadIGpackage(ig_url, fhir_server, params)
if err != nil {
return err
}
return selectSetup()
}
func selectDefaultOrCustom() error {
prompt := promptui.Select{
Label: "Great, now choose an installation type",
Items: []string{"Default Install Options", "Custom Install Options", "Quit", "Back"},
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectDefaultOrCustom() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "Default Install Options":
err = selectDefaultAction()
case "Custom Install Options":
err = selectCustomOptions()
case "Quit":
quit()
case "Back":
err = selectSetup()
}
return err
}
func selectCustomOptions() error {
index := 1
items := []string{
"Choose deploy action (default is init)",
"Specify deploy packages",
"Specify environment variable file location",
"Specify environment variables",
"Specify custom package locations",
"Toggle only flag",
"Specify Instant Version",
"Toggle dev mode (default mode is prod)",
"Execute with current options",
"View current options set",
"Reset to default options",
"Help",
"Quit",
"Back",
}
if !cfg.DisableCustomTargetSelection {
items = append(items[:index+1], items[index:]...)
items[index] = "Choose target launcher (default is " + cfg.DefaultTargetLauncher + ")"
}
prompt := promptui.Select{
Label: "Great, now choose an action",
Items: items,
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectCustomOptions() prompt failed")
}
switch result {
case "Choose deploy action (default is init)":
err = setStartupAction()
case "Choose target launcher (default is " + cfg.DefaultTargetLauncher + ")":
err = setTargetLauncher()
case "Specify deploy packages":
err = setStartupPackages()
case "Specify environment variable file location":
err = setEnvVarFileLocation()
case "Specify environment variables":
err = setEnvVars()
case "Specify custom package locations":
err = setCustomPackages()
case "Toggle only flag":
err = toggleOnlyFlag()
case "Toggle dev mode (default mode is prod)":
err = toggleDevMode()
case "Specify Instant Version":
err = setInstantVersion()
case "Execute with current options":
err = printAll(false)
if err != nil {
return err
}
err = executeCommand()
case "View current options set":
err = printAll(true)
case "Reset to default options":
resetAll()
err = printAll(true)
case "Help":
fmt.Println(getHelpText(true, "Custom Options"))
return selectCustomOptions()
case "Quit":
quit()
case "Back":
err = selectDefaultOrCustom()
}
return err
}
func resetAll() {
customOptions.startupAction = "init"
customOptions.startupPackages = make([]string, 0)
customOptions.envVarFileLocation = ""
customOptions.envVars = make([]string, 0)
customOptions.customPackageFileLocations = make([]string, 0)
customOptions.onlyFlag = false
customOptions.instantVersion = "latest"
customOptions.targetLauncher = cfg.DefaultTargetLauncher
customOptions.devMode = false
fmt.Println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\nAll custom options have been reset to default.\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
}
func setStartupAction() error {
prompt := promptui.Select{
Label: "Great, now choose a deploy action",
Items: []string{"init", "destroy", "up", "down", "test", "Help", "Quit", "Back"},
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setStartupAction() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "init", "destroy", "up", "down", "test":
customOptions.startupAction = result
err = selectCustomOptions()
case "Help":
fmt.Println(getHelpText(true, "Deploy Commands"))
return setStartupAction()
case "Quit":
quit()
case "Back":
err = selectCustomOptions()
}
return err
}
func setTargetLauncher() error {
prompt := promptui.Select{
Label: "Choose a target launcher",
Items: []string{"docker", "swarm", "kubernetes", "Quit", "Back"},
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setTargetLauncher() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "docker", "swarm", "kubernetes":
customOptions.targetLauncher = result
err = selectCustomOptions()
case "Quit":
quit()
case "Back":
err = selectCustomOptions()
}
return err
}
var DeployCommands []string
func executeCommand() error {
DeployCommands = []string{customOptions.startupAction}
if len(customOptions.startupPackages) == 0 {
fmt.Printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" +
"Warning: No package IDs specified, all default packages will be included in your command.\n" +
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n")
}
DeployCommands = append(DeployCommands, customOptions.startupPackages...)
if customOptions.envVarFileLocation != "" && len(customOptions.envVarFileLocation) > 0 {
DeployCommands = append(DeployCommands, "--env-file="+customOptions.envVarFileLocation)
}
if customOptions.envVars != nil && len(customOptions.envVars) > 0 {
for _, e := range customOptions.envVars {
DeployCommands = append(DeployCommands, "-e="+e)
}
}
if customOptions.customPackageFileLocations != nil && len(customOptions.customPackageFileLocations) > 0 {
for _, c := range customOptions.customPackageFileLocations {
DeployCommands = append(DeployCommands, "-c="+c)
}
}
if customOptions.onlyFlag {
DeployCommands = append(DeployCommands, "--only")
}
if customOptions.devMode {
DeployCommands = append(DeployCommands, "--dev")
}
DeployCommands = append(DeployCommands, "--instant-version="+customOptions.instantVersion)
DeployCommands = append(DeployCommands, "-t="+customOptions.targetLauncher)
return runDeployCommand(DeployCommands)
}
func printSlice(slice []string) {
for _, s := range slice {
fmt.Printf("-%q\n", s)
}
fmt.Println()
}
func printAll(loopback bool) error {
fmt.Println("\nCurrent Custom Options Specified\n---------------------------------")
fmt.Println("Target Launcher:")
fmt.Printf("-%q\n", customOptions.targetLauncher)
fmt.Println("Startup Action:")
fmt.Printf("-%q\n", customOptions.startupAction)
fmt.Println("Startup Packages:")
if customOptions.startupPackages != nil && len(customOptions.startupPackages) > 0 {
printSlice(customOptions.startupPackages)
}
fmt.Println("Environment Variable File Path:")
if customOptions.envVarFileLocation != "" && len(customOptions.envVarFileLocation) > 0 {
fmt.Printf("-%q\n", customOptions.envVarFileLocation)
}
fmt.Println("Environment Variables:")
if customOptions.envVars != nil && len(customOptions.envVars) > 0 {
printSlice(customOptions.envVars)
}
if customOptions.customPackageFileLocations != nil && len(customOptions.customPackageFileLocations) > 0 {
fmt.Println("Custom Packages:")
printSlice(customOptions.customPackageFileLocations)
}
fmt.Println("Instant Image Version:")
fmt.Printf("-%q\n", customOptions.instantVersion)
fmt.Println("Only Flag Setting:")
if customOptions.onlyFlag {
fmt.Printf("-%q\n", "On")
} else {
fmt.Printf("-%q\n", "Off")
}
fmt.Println("Dev Mode Setting:")
if customOptions.devMode {
fmt.Printf("-%q\n\n", "On")
} else {
fmt.Printf("-%q\n\n", "Off")
}
var err error
if loopback {
err = selectCustomOptions()
}
return err
}
func setStartupPackages() error {
if customOptions.startupPackages != nil && len(customOptions.startupPackages) > 0 {
fmt.Println("\nCurrent Startup Packages Specified:")
printSlice(customOptions.startupPackages)
}
prompt := promptui.Prompt{
Label: "Startup Package List(Comma Delimited). e.g. core,cdr",
}
packageList, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setStartupPackages() prompt failed")
}
startupPackages := strings.Split(packageList, ",")
for _, p := range startupPackages {
if !sliceContains(customOptions.startupPackages, p) {
customOptions.startupPackages = append(customOptions.startupPackages, p)
} else {
fmt.Printf(p + " package already exists in the list.\n")
}
}
return selectCustomOptions()
}
func setCustomPackages() error {
if customOptions.customPackageFileLocations != nil && len(customOptions.customPackageFileLocations) > 0 {
fmt.Println("Current Custom Packages Specified:")
printSlice(customOptions.customPackageFileLocations)
}
prompt := promptui.Prompt{
Label: "Custom Package List(Comma Delimited). e.g. " + filepath.FromSlash("../project/cdr") + "," + filepath.FromSlash("../project/demo"),
}
customPackageList, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setCustomPackages() prompt failed")
}
newCustomPackages := strings.Split(customPackageList, ",")
for _, cp := range newCustomPackages {
if strings.HasPrefix(cp, "http") || strings.HasPrefix(cp, "git") {
if !sliceContains(customOptions.customPackageFileLocations, cp) {
customOptions.customPackageFileLocations = append(customOptions.customPackageFileLocations, cp)
} else {
fmt.Printf(cp + " URL already exists in the list.\n")
}
} else {
exists, fileErr := fileExists(cp)
if exists {
if !sliceContains(customOptions.customPackageFileLocations, cp) {
customOptions.customPackageFileLocations = append(customOptions.customPackageFileLocations, cp)
} else {
fmt.Printf(cp + " path already exists in the list.\n")
}
} else {
fmt.Printf("\nFile at location %q could not be found due to error: %v\n", cp, fileErr)
fmt.Println("\n-----------------\nPlease try again.\n-----------------")
}
}
}
return selectCustomOptions()
}
func setEnvVarFileLocation() error {
if customOptions.envVarFileLocation != "" && len(customOptions.envVarFileLocation) > 0 {
fmt.Println("Current Environment Variable File Location Specified:")
fmt.Printf("-%q\n", customOptions.envVarFileLocation)
}
prompt := promptui.Prompt{
Label: "Environment Variable file location e.g. " + filepath.FromSlash("../project/prod.env"),
}
envVarFileLocation, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setEnvVarFileLocation() prompt failed")
}
exists, fileErr := fileExists(envVarFileLocation)
if exists {
customOptions.envVarFileLocation = envVarFileLocation
} else {
fmt.Printf("\nFile at location %q could not be found due to error: %v\n", envVarFileLocation, fileErr)
fmt.Println("\n-----------------\nPlease try again.\n-----------------")
}
return selectCustomOptions()
}
func setInstantVersion() error {
if customOptions.instantVersion != "latest" && len(customOptions.instantVersion) > 0 {
fmt.Println("Current Instant OpenHIE Image Version Specified:")
fmt.Printf("-%q\n", customOptions.instantVersion)
}
prompt := promptui.Prompt{
Label: "Instant OpenHIE Image Version e.g. 0.0.9",
}
instantVersion, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setInstantVersion() prompt failed")
}
customOptions.instantVersion = instantVersion
return selectCustomOptions()
}
func setEnvVars() error {
if customOptions.envVars != nil && len(customOptions.envVars) > 0 {
fmt.Println("Current Environment Variables Specified:")
printSlice(customOptions.envVars)
}
prompt := promptui.Prompt{
Label: "Environment Variable List(Comma Delimited). e.g. NODE_ENV=PROD,DOMAIN_NAME=instant.com",
}
envVarList, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "setEnvVars() prompt failed")
}
newEnvVars := strings.Split(envVarList, ",")
for _, env := range newEnvVars {
if !sliceContains(customOptions.envVars, env) {
customOptions.envVars = append(customOptions.envVars, env)
} else {
fmt.Printf(env + " environment variable already exists in the list.\n")
}
}
return selectCustomOptions()
}
func toggleOnlyFlag() error {
customOptions.onlyFlag = !customOptions.onlyFlag
if customOptions.onlyFlag {
fmt.Println("Only flag is now on")
} else {
fmt.Println("Only flag is now off")
}
return selectCustomOptions()
}
func toggleDevMode() error {
customOptions.devMode = !customOptions.devMode
if customOptions.devMode {
fmt.Println("Dev mode is now on")
} else {
fmt.Println("Dev mode is now off")
}
return selectCustomOptions()
}
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, err
}
return false, err
}
func selectDefaultAction() error {
prompt := promptui.Select{
Label: "Great, now choose an action",
Items: []string{
"init",
"down",
"destroy",
"up",
"Help",
"Back",
"Quit",
},
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectDefaultAction() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "Help":
fmt.Println(getHelpText(true, "Deploy Commands"))
return selectDefaultAction()
case "Back":
return selectDefaultOrCustom()
case "Quit":
quit()
return nil
}
return selectDefaultPackage(result)
}
func selectDefaultPackage(action string) error {
var optionItems []string
for _, p := range cfg.Packages {
optionItems = append(optionItems, p.Name)
}
optionItems = append(optionItems, "All", "Back", "Quit")
prompt := promptui.Select{
Label: "Which package would you like to perform the action on (Packages will also invoke their dependencies automatically)",
Items: optionItems,
Size: 12,
}
i, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectDefaultPackage() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "All":
fmt.Println("...Setting up All Packages")
err = RunDeployCommand([]string{action, "-t=" + cfg.DefaultTargetLauncher})
if err != nil {
return err
}
return selectDefaultAction()
case "Quit":
quit()
return nil
case "Back":
return selectDefaultAction()
default:
err = RunDeployCommand([]string{cfg.Packages[i].ID, action, "-t=" + cfg.DefaultTargetLauncher})
if err != nil {
return err
}
return selectDefaultAction()
}
}
func selectPackageCluster() error {
prompt := promptui.Select{
Label: "Great, now choose an action",
Items: []string{"Initialise Core (Required, Start Here)", "Launch Facility Registry", "Launch Workforce", "Stop and Cleanup Core", "Stop and Cleanup Facility Registry", "Stop and Cleanup Workforce", "Stop All Services and Cleanup Kubernetes", "Quit", "Back"},
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return errors.Wrap(err, "selectPackageCluster() prompt failed")
}
fmt.Printf("\nYou chose %q\n========================================\n", result)
switch result {
case "Launch Core (Required, Start Here)":
fmt.Println("...Setting up Core Package")
err = RunDeployCommand([]string{"core", "init", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Launch Facility Registry":
fmt.Println("...Setting up Facility Registry Package")
err = RunDeployCommand([]string{"facility", "up", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Launch Workforce":
fmt.Println("...Setting up Workforce Package")
err = RunDeployCommand([]string{"healthworker", "up", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Stop and Cleanup Core":
fmt.Println("Stopping and Cleaning Up Core...")
err = RunDeployCommand([]string{"core", "destroy", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Stop and Cleanup Facility Registry":
fmt.Println("Stopping and Cleaning Up Facility Registry...")
err = RunDeployCommand([]string{"facility", "destroy", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Stop and Cleanup Workforce":
fmt.Println("Stopping and Cleaning Up Workforce...")
err = RunDeployCommand([]string{"healthworker", "destroy", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Stop All Services and Cleanup Kubernetes":
fmt.Println("Stopping and Cleaning Up Everything...")
err = RunDeployCommand([]string{"core", "destroy", "-t=k8s"})
if err != nil {
return err
}
err = RunDeployCommand([]string{"facility", "destroy", "-t=k8s"})
if err != nil {
return err
}
err = RunDeployCommand([]string{"healthworker", "destroy", "-t=k8s"})
if err != nil {
return err
}
err = selectPackageCluster()
case "Quit":
quit()
case "Back":
err = selectSetup()
}
return err
}
func selectFHIR() (result_url string, params *Params, err error) {
prompt := promptui.Select{
Label: "Select or enter URL for a FHIR Server",
Items: []string{"Docker Default", "Kubernetes Default", "Use Public HAPI Server", "Enter a Server URL", "Quit", "Back"},
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return "", nil, errors.Wrap(err, "selectFHIR() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "Docker Default":
result_url := "http://localhost:8080/fhir"
params := &Params{}
params.TypeAuth = "Custom"
params.Token = "test"
return result_url, params, nil
case "Kubernetes Default":
result_url := "http://localhost:8080/fhir"
params := &Params{}
params.TypeAuth = "Custom"
params.Token = "test"
return result_url, params, nil
case "Use Public HAPI Server":
result_url := "http://hapi.fhir.org/baseR4"
params := &Params{}
params.TypeAuth = "None"
return result_url, params, nil
case "Enter a Server URL":
prompt := promptui.Prompt{
Label: "URL",
}
result_url, err := prompt.Run()
if err != nil {
return "", nil, errors.Wrap(err, "Server URL in selectFHIR() prompt failed")
}
params, err := selectParams()
return result_url, params, err
case "Quit":
quit()
params := &Params{}
return "", params, nil
case "Back":
err = selectUtil()
params := &Params{}
return "", params, nil
}
return result_url, params, nil
}
type Params struct {
// none, token, basic, custom
TypeAuth string
Token string
BasicUser string
BasicPass string
}
func selectParams() (*Params, error) {
a := &Params{}
prompt := promptui.Select{
Label: "Choose authentication type",
Items: []string{"None", "Basic", "Token", "Custom", "Quit", "Back"},
Size: 12,
}
_, result, err := prompt.Run()
if err != nil {
return nil, errors.Wrap(err, "selectParams() prompt failed")
}
fmt.Printf("You chose %q\n========================================\n", result)
switch result {
case "None":
a.TypeAuth = "None"
return a, nil
case "Basic":
a.TypeAuth = "Basic"
prompt_basic_user := promptui.Prompt{
Label: "Basic User",
}
result_basic_user, err := prompt_basic_user.Run()
if err != nil {
return nil, errors.Wrap(err, "Case 'Basic' in selectParams() prompt failed")
}
a.BasicUser = result_basic_user
prompt_basic_pass := promptui.Prompt{
Label: "Basic Password",
}
result_basic_pass, err := prompt_basic_pass.Run()
if err != nil {
return nil, errors.Wrap(err, "Case 'Basic' in selectParams() prompt failed")
}
a.BasicPass = result_basic_pass
return a, nil
case "Token":
a.TypeAuth = "Token"
prompt_token := promptui.Prompt{
Label: "Bearer Token",
}
result_token, err := prompt_token.Run()
if err != nil {
return nil, errors.Wrap(err, "Case 'Token' in selectParams() prompt failed")
}
a.Token = result_token
return a, nil
case "Custom":
a.TypeAuth = "Custom"
prompt_ctoken := promptui.Prompt{
Label: "Custom Token",
}
result_ctoken, err := prompt_ctoken.Run()
if err != nil {
return nil, errors.Wrap(err, "Case 'Custom' in selectParams() prompt failed")
}
a.Token = result_ctoken
return a, nil
case "Quit":
quit()
return a, nil
case "Back":
err = selectUtil()
return a, nil
}
return a, err
}