-
Notifications
You must be signed in to change notification settings - Fork 55
/
prompts.go
860 lines (766 loc) · 21.3 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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package prompts
import (
"errors"
"fmt"
"math/big"
"net/url"
"strconv"
"time"
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanchego/ids"
"github.com/ethereum/go-ethereum/common"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"golang.org/x/mod/semver"
)
const (
Yes = "Yes"
No = "No"
Add = "Add"
Del = "Delete"
Preview = "Preview"
MoreInfo = "More Info"
Done = "Done"
Cancel = "Cancel"
LessThanEq = "Less Than Or Eq"
MoreThanEq = "More Than Or Eq"
MoreThan = "More Than"
NotEq = "Not Eq"
)
var errNoKeys = errors.New("no keys")
type Comparator struct {
Label string // Label that identifies reference value
Type string // Less Than Eq or More than Eq
Value uint64 // Value to Compare To
}
func (comparator *Comparator) Validate(val uint64) error {
switch comparator.Type {
case LessThanEq:
if val > comparator.Value {
return fmt.Errorf(fmt.Sprintf("the value must be smaller than or equal to %s (%d)", comparator.Label, comparator.Value))
}
case MoreThan:
if val <= comparator.Value {
return fmt.Errorf(fmt.Sprintf("the value must be bigger than %s (%d)", comparator.Label, comparator.Value))
}
case MoreThanEq:
if val < comparator.Value {
return fmt.Errorf(fmt.Sprintf("the value must be bigger than or equal to %s (%d)", comparator.Label, comparator.Value))
}
case NotEq:
if val == comparator.Value {
return fmt.Errorf(fmt.Sprintf("the value must be different than %s (%d)", comparator.Label, comparator.Value))
}
}
return nil
}
type Prompter interface {
CapturePositiveBigInt(promptStr string) (*big.Int, error)
CaptureAddress(promptStr string) (common.Address, error)
CaptureNewFilepath(promptStr string) (string, error)
CaptureExistingFilepath(promptStr string) (string, error)
CaptureYesNo(promptStr string) (bool, error)
CaptureNoYes(promptStr string) (bool, error)
CaptureList(promptStr string, options []string) (string, error)
CaptureListWithSize(promptStr string, options []string, size int) (string, error)
CaptureString(promptStr string) (string, error)
CaptureValidatedString(promptStr string, validator func(string) error) (string, error)
CaptureURL(promptStr string, validateConnection bool) (string, error)
CaptureRepoBranch(promptStr string, repo string) (string, error)
CaptureRepoFile(promptStr string, repo string, branch string) (string, error)
CaptureGitURL(promptStr string) (*url.URL, error)
CaptureStringAllowEmpty(promptStr string) (string, error)
CaptureEmail(promptStr string) (string, error)
CaptureIndex(promptStr string, options []any) (int, error)
CaptureVersion(promptStr string) (string, error)
CaptureFujiDuration(promptStr string) (time.Duration, error)
CaptureMainnetDuration(promptStr string) (time.Duration, error)
CaptureDate(promptStr string) (time.Time, error)
CaptureNodeID(promptStr string) (ids.NodeID, error)
CaptureID(promptStr string) (ids.ID, error)
CaptureWeight(promptStr string) (uint64, error)
CapturePositiveInt(promptStr string, comparators []Comparator) (int, error)
CaptureInt(promptStr string) (int, error)
CaptureUint32(promptStr string) (uint32, error)
CaptureUint64(promptStr string) (uint64, error)
CaptureFloat(promptStr string, validator func(float64) error) (float64, error)
CaptureUint64Compare(promptStr string, comparators []Comparator) (uint64, error)
CapturePChainAddress(promptStr string, network models.Network) (string, error)
CaptureXChainAddress(promptStr string, network models.Network) (string, error)
CaptureFutureDate(promptStr string, minDate time.Time) (time.Time, error)
ChooseKeyOrLedger(goal string) (bool, error)
}
type realPrompter struct{}
// NewProcessChecker creates a new process checker which can respond if the server is running
func NewPrompter() Prompter {
return &realPrompter{}
}
// CaptureListDecision runs a for loop and continuously asks the
// user for a specific input (currently only `CapturePChainAddress`
// and `CaptureAddress` is supported) until the user cancels or
// chooses `Done`. It does also offer an optional `info` to print
// (if provided) and a preview. Items can also be removed.
func CaptureListDecision[T comparable](
// we need this in order to be able to run mock tests
prompter Prompter,
// the main prompt for entering address keys
prompt string,
// the Capture function to use
capture func(prompt string) (T, error),
// the prompt for each address
capturePrompt string,
// label describes the entity we are prompting for (e.g. address, control key, etc.)
label string,
// optional parameter to allow the user to print the info string for more information
info string,
) ([]T, bool, error) {
finalList := []T{}
for {
listDecision, err := prompter.CaptureList(
prompt, []string{Add, Del, Preview, MoreInfo, Done, Cancel},
)
if err != nil {
return nil, false, err
}
switch listDecision {
case Add:
elem, err := capture(capturePrompt)
if err != nil {
return nil, false, err
}
if contains(finalList, elem) {
fmt.Println(label + " already in list")
continue
}
finalList = append(finalList, elem)
case Del:
if len(finalList) == 0 {
fmt.Println("No " + label + " added yet")
continue
}
finalListAnyT := []any{}
for _, v := range finalList {
finalListAnyT = append(finalListAnyT, v)
}
index, err := prompter.CaptureIndex("Choose element to remove:", finalListAnyT)
if err != nil {
return nil, false, err
}
finalList = append(finalList[:index], finalList[index+1:]...)
case Preview:
if len(finalList) == 0 {
fmt.Println("The list is empty")
break
}
for i, k := range finalList {
fmt.Printf("%d. %v\n", i, k)
}
case MoreInfo:
if info != "" {
fmt.Println(info)
}
case Done:
return finalList, false, nil
case Cancel:
return nil, true, nil
default:
return nil, false, errors.New("unexpected option")
}
}
}
func (*realPrompter) CaptureFujiDuration(promptStr string) (time.Duration, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateFujiStakingDuration,
}
durationStr, err := prompt.Run()
if err != nil {
return 0, err
}
return time.ParseDuration(durationStr)
}
func (*realPrompter) CaptureMainnetDuration(promptStr string) (time.Duration, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateMainnetStakingDuration,
}
durationStr, err := prompt.Run()
if err != nil {
return 0, err
}
return time.ParseDuration(durationStr)
}
func (*realPrompter) CaptureDate(promptStr string) (time.Time, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateTime,
}
timeStr, err := prompt.Run()
if err != nil {
return time.Time{}, err
}
return time.Parse(constants.TimeParseLayout, timeStr)
}
func (*realPrompter) CaptureID(promptStr string) (ids.ID, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateID,
}
idStr, err := prompt.Run()
if err != nil {
return ids.Empty, err
}
return ids.FromString(idStr)
}
func (*realPrompter) CaptureNodeID(promptStr string) (ids.NodeID, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateNodeID,
}
nodeIDStr, err := prompt.Run()
if err != nil {
return ids.EmptyNodeID, err
}
return ids.NodeIDFromString(nodeIDStr)
}
func (*realPrompter) CaptureWeight(promptStr string) (uint64, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateWeight,
}
amountStr, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.ParseUint(amountStr, 10, 64)
}
func (*realPrompter) CaptureInt(promptStr string) (int, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(input string) error {
_, err := strconv.Atoi(input)
if err != nil {
return err
}
return nil
},
}
input, err := prompt.Run()
if err != nil {
return 0, err
}
val, err := strconv.Atoi(input)
if err != nil {
return 0, err
}
return val, nil
}
func (*realPrompter) CaptureUint32(promptStr string) (uint32, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(input string) error {
_, err := strconv.ParseUint(input, 0, 32)
if err != nil {
return err
}
return nil
},
}
input, err := prompt.Run()
if err != nil {
return 0, err
}
val, err := strconv.ParseUint(input, 0, 32)
if err != nil {
return 0, err
}
return uint32(val), nil
}
func (*realPrompter) CaptureUint64(promptStr string) (uint64, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateBiggerThanZero,
}
amountStr, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.ParseUint(amountStr, 0, 64)
}
func (*realPrompter) CaptureFloat(promptStr string, validator func(float64) error) (float64, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(input string) error {
val, err := strconv.ParseFloat(input, 64)
if err != nil {
return err
}
return validator(val)
},
}
amountStr, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.ParseFloat(amountStr, 64)
}
func (*realPrompter) CapturePositiveInt(promptStr string, comparators []Comparator) (int, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(input string) error {
val, err := strconv.Atoi(input)
if err != nil {
return err
}
if val < 0 {
return errors.New("input is less than 0")
}
for _, comparator := range comparators {
if err := comparator.Validate(uint64(val)); err != nil {
return err
}
}
return nil
},
}
amountStr, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.Atoi(amountStr)
}
func (*realPrompter) CaptureUint64Compare(promptStr string, comparators []Comparator) (uint64, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(input string) error {
val, err := strconv.ParseUint(input, 0, 64)
if err != nil {
return err
}
for _, comparator := range comparators {
if err := comparator.Validate(val); err != nil {
return err
}
}
return nil
},
}
amountStr, err := prompt.Run()
if err != nil {
return 0, err
}
return strconv.ParseUint(amountStr, 0, 64)
}
func (*realPrompter) CapturePositiveBigInt(promptStr string) (*big.Int, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validatePositiveBigInt,
}
amountStr, err := prompt.Run()
if err != nil {
return nil, err
}
amountInt := new(big.Int)
amountInt, ok := amountInt.SetString(amountStr, 10)
if !ok {
return nil, errors.New("SetString: error")
}
return amountInt, nil
}
func (*realPrompter) CapturePChainAddress(promptStr string, network models.Network) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: getPChainValidationFunc(network),
}
return prompt.Run()
}
func (*realPrompter) CaptureXChainAddress(promptStr string, network models.Network) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: getXChainValidationFunc(network),
}
return prompt.Run()
}
func (*realPrompter) CaptureAddress(promptStr string) (common.Address, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateAddress,
}
addressStr, err := prompt.Run()
if err != nil {
return common.Address{}, err
}
addressHex := common.HexToAddress(addressStr)
return addressHex, nil
}
func (*realPrompter) CaptureExistingFilepath(promptStr string) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateExistingFilepath,
}
pathStr, err := prompt.Run()
if err != nil {
return "", err
}
return pathStr, nil
}
func (*realPrompter) CaptureNewFilepath(promptStr string) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateNewFilepath,
}
pathStr, err := prompt.Run()
if err != nil {
return "", err
}
return pathStr, nil
}
func yesNoBase(promptStr string, orderedOptions []string) (bool, error) {
prompt := promptui.Select{
Label: promptStr,
Items: orderedOptions,
}
_, decision, err := prompt.Run()
if err != nil {
return false, err
}
return decision == Yes, nil
}
func (*realPrompter) CaptureYesNo(promptStr string) (bool, error) {
return yesNoBase(promptStr, []string{Yes, No})
}
func (*realPrompter) CaptureNoYes(promptStr string) (bool, error) {
return yesNoBase(promptStr, []string{No, Yes})
}
func (*realPrompter) CaptureList(promptStr string, options []string) (string, error) {
prompt := promptui.Select{
Label: promptStr,
Items: options,
}
_, listDecision, err := prompt.Run()
if err != nil {
return "", err
}
return listDecision, nil
}
func (*realPrompter) CaptureListWithSize(promptStr string, options []string, size int) (string, error) {
prompt := promptui.Select{
Label: promptStr,
Items: options,
Size: size,
}
_, listDecision, err := prompt.Run()
if err != nil {
return "", err
}
return listDecision, nil
}
func (*realPrompter) CaptureEmail(promptStr string) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateEmail,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
return str, nil
}
func (*realPrompter) CaptureStringAllowEmpty(promptStr string) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
return str, nil
}
func (*realPrompter) CaptureURL(promptStr string, validateConnection bool) (string, error) {
for {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateURLFormat,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
if !validateConnection {
return str, nil
}
if err := ValidateURL(str); err == nil {
return str, nil
}
ux.Logger.PrintToUser("Invalid URL: %s", err)
}
}
func (*realPrompter) CaptureRepoBranch(promptStr string, repo string) (string, error) {
for {
var err error
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateNonEmpty,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
if err = ValidateRepoBranch(repo, str); err == nil {
return str, nil
}
ux.Logger.PrintToUser("Invalid Repo Branch: %s", err)
}
}
func (*realPrompter) CaptureRepoFile(promptStr string, repo string, branch string) (string, error) {
for {
var err error
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateNonEmpty,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
if err = ValidateRepoFile(repo, branch, str); err == nil {
return str, nil
}
ux.Logger.PrintToUser("Invalid Repo File: %s", err)
}
}
func (*realPrompter) CaptureString(promptStr string) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateNonEmpty,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
return str, nil
}
func (*realPrompter) CaptureValidatedString(promptStr string, validator func(string) error) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validator,
}
str, err := prompt.Run()
if err != nil {
return "", err
}
return str, nil
}
func (*realPrompter) CaptureGitURL(promptStr string) (*url.URL, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateURLFormat,
}
str, err := prompt.Run()
if err != nil {
return nil, err
}
parsedURL, err := url.ParseRequestURI(str)
if err != nil {
return nil, err
}
return parsedURL, nil
}
func (*realPrompter) CaptureVersion(promptStr string) (string, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(input string) error {
if !semver.IsValid(input) {
return errors.New("version must be a legal semantic version (ex: v1.1.1)")
}
return nil
},
}
str, err := prompt.Run()
if err != nil {
return "", err
}
return str, nil
}
func (*realPrompter) CaptureIndex(promptStr string, options []any) (int, error) {
prompt := promptui.Select{
Label: promptStr,
Items: options,
}
listIndex, _, err := prompt.Run()
if err != nil {
return 0, err
}
return listIndex, nil
}
// CaptureFutureDate requires from the user a date input which is in the future.
// If `minDate` is not empty, the minimum time in the future from the provided date is required
// Otherwise, time from time.Now() is chosen.
func (*realPrompter) CaptureFutureDate(promptStr string, minDate time.Time) (time.Time, error) {
prompt := promptui.Prompt{
Label: promptStr,
Validate: func(s string) error {
t, err := time.Parse(constants.TimeParseLayout, s)
if err != nil {
return err
}
if minDate == (time.Time{}) {
minDate = time.Now()
}
if t.Before(minDate.UTC()) {
return fmt.Errorf("the provided date is before %s UTC", minDate.Format(constants.TimeParseLayout))
}
return nil
},
}
timestampStr, err := prompt.Run()
if err != nil {
return time.Time{}, err
}
return time.Parse(constants.TimeParseLayout, timestampStr)
}
// returns true [resp. false] if user chooses stored key [resp. ledger] option
func (prompter *realPrompter) ChooseKeyOrLedger(goal string) (bool, error) {
const (
keyOption = "Use stored key"
ledgerOption = "Use ledger"
)
option, err := prompter.CaptureList(
fmt.Sprintf("Which key source should be used to %s?", goal),
[]string{keyOption, ledgerOption},
)
if err != nil {
return false, err
}
return option == keyOption, nil
}
func contains[T comparable](list []T, element T) bool {
for _, val := range list {
if val == element {
return true
}
}
return false
}
// check subnet authorization criteria:
// - [subnetAuthKeys] satisfy subnet's [threshold]
// - [subnetAuthKeys] is a subset of subnet's [controlKeys]
func CheckSubnetAuthKeys(walletKeys []string, subnetAuthKeys []string, controlKeys []string, threshold uint32) error {
for _, walletKey := range walletKeys {
if slices.Contains(controlKeys, walletKey) && !slices.Contains(subnetAuthKeys, walletKey) {
return fmt.Errorf("wallet key %s is a subnet control key so it must be included in subnet auth keys", walletKey)
}
}
if len(subnetAuthKeys) != int(threshold) {
return fmt.Errorf("number of given subnet auth differs from the threshold")
}
for _, subnetAuthKey := range subnetAuthKeys {
found := false
for _, controlKey := range controlKeys {
if subnetAuthKey == controlKey {
found = true
break
}
}
if !found {
return fmt.Errorf("subnet auth key %s does not belong to control keys", subnetAuthKey)
}
}
return nil
}
// get subnet authorization keys from the user, as a subset of the subnet's [controlKeys]
// with a len equal to the subnet's [threshold]
func GetSubnetAuthKeys(prompt Prompter, walletKeys []string, controlKeys []string, threshold uint32) ([]string, error) {
if len(controlKeys) == int(threshold) {
return controlKeys, nil
}
subnetAuthKeys := []string{}
filteredControlKeys := []string{}
filteredControlKeys = append(filteredControlKeys, controlKeys...)
for _, walletKey := range walletKeys {
if slices.Contains(controlKeys, walletKey) {
ux.Logger.PrintToUser("Adding wallet key %s to the tx subnet auth keys as it is a subnet control key", walletKey)
subnetAuthKeys = append(subnetAuthKeys, walletKey)
index, err := utils.GetIndexInSlice(filteredControlKeys, walletKey)
if err != nil {
return nil, err
}
filteredControlKeys = append(filteredControlKeys[:index], filteredControlKeys[index+1:]...)
}
}
for len(subnetAuthKeys) != int(threshold) {
subnetAuthKey, err := prompt.CaptureList(
"Choose a subnet auth key",
filteredControlKeys,
)
if err != nil {
return nil, err
}
index, err := utils.GetIndexInSlice(filteredControlKeys, subnetAuthKey)
if err != nil {
return nil, err
}
subnetAuthKeys = append(subnetAuthKeys, subnetAuthKey)
filteredControlKeys = append(filteredControlKeys[:index], filteredControlKeys[index+1:]...)
}
return subnetAuthKeys, nil
}
func GetFujiKeyOrLedger(prompt Prompter, goal string, keyDir string) (bool, string, error) {
useStoredKey, err := prompt.ChooseKeyOrLedger(goal)
if err != nil {
return false, "", err
}
if !useStoredKey {
return true, "", nil
}
keyName, err := CaptureKeyName(prompt, goal, keyDir, true)
if err != nil {
if errors.Is(err, errNoKeys) {
ux.Logger.PrintToUser("No private keys have been found. Create a new one with `avalanche key create`")
}
return false, "", err
}
return false, keyName, nil
}
func CaptureKeyName(prompt Prompter, goal string, keyDir string, addEwoq bool) (string, error) {
keyNames, err := utils.GetKeyNames(keyDir, addEwoq)
if err != nil {
return "", err
}
if len(keyNames) == 0 {
return "", errNoKeys
}
size := len(keyNames)
if size > 10 {
size = 10
}
keyName, err := prompt.CaptureListWithSize(fmt.Sprintf("Which stored key should be used to %s?", goal), keyNames, size)
if err != nil {
return "", err
}
return keyName, nil
}
func CaptureBoolFlag(
prompt Prompter,
cmd *cobra.Command,
flagName string,
flagValue bool,
promptMsg string,
) (bool, error) {
if flagValue {
return true, nil
}
if flag := cmd.Flags().Lookup(flagName); flag == nil {
return false, fmt.Errorf("flag configuration %q not found for cmd %q", flagName, cmd.Use)
} else if !flag.Changed {
return prompt.CaptureYesNo(promptMsg)
} else {
return cmd.Flags().GetBool(flagName)
}
}