-
Notifications
You must be signed in to change notification settings - Fork 50
/
prompt.go
1007 lines (949 loc) · 28.8 KB
/
prompt.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// © 2022 Nokia.
//
// This code is a Contribution to the gNMIc project (“Work”) made under the Google Software Grant and Corporate Contributor License Agreement (“CLA”) and governed by the Apache License 2.0.
// No other rights or licenses in or to any of Nokia’s intellectual property are granted for any other purpose.
// This code is provided on an “as is” basis without any warranties of any kind.
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"errors"
"fmt"
"os"
"sort"
"strings"
"unicode"
goprompt "github.com/c-bata/go-prompt"
"github.com/c-bata/go-prompt/completer"
homedir "github.com/mitchellh/go-homedir"
"github.com/nsf/termbox-go"
"github.com/olekukonko/tablewriter"
"github.com/openconfig/goyang/pkg/yang"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openconfig/gnmic/pkg/api/types"
"github.com/openconfig/gnmic/pkg/cmd/get"
"github.com/openconfig/gnmic/pkg/cmd/subscribe"
)
var colorMapping = map[string]goprompt.Color{
"black": goprompt.Black,
"dark_red": goprompt.DarkRed,
"dark_green": goprompt.DarkGreen,
"brown": goprompt.Brown,
"dark_blue": goprompt.DarkBlue,
"purple": goprompt.Purple,
"cyan": goprompt.Cyan,
"light_gray": goprompt.LightGray,
"dark_gray": goprompt.DarkGray,
"red": goprompt.Red,
"green": goprompt.Green,
"yellow": goprompt.Yellow,
"blue": goprompt.Blue,
"fuchsia": goprompt.Fuchsia,
"turquoise": goprompt.Turquoise,
"white": goprompt.White,
}
var targetListHeader = []string{
"Name", "Address", "Username", "Password", "Insecure", "Skip Verify", "TLS CA", "TLS Certificate", "TLS Key"}
var subscriptionListHeader = []string{"Name", "Mode", "Prefix", "Paths", "Interval", "Encoding"}
func getColor(flagName string) goprompt.Color {
switch flagName {
case "prefix-color":
if cgoprompt, ok := colorMapping[gApp.Config.LocalFlags.PromptPrefixColor]; ok {
return cgoprompt
}
case "suggestions-bg-color":
if cgoprompt, ok := colorMapping[gApp.Config.LocalFlags.PromptSuggestionsBGColor]; ok {
return cgoprompt
}
case "description-bg-color":
if cgoprompt, ok := colorMapping[gApp.Config.LocalFlags.PromptDescriptionBGColor]; ok {
return cgoprompt
}
}
defColor := "yellow"
promptModeCmd.Flags().VisitAll(
func(f *pflag.Flag) {
if f.Name == flagName {
defColor = f.DefValue
return
}
},
)
return colorMapping[defColor]
}
var promptModeCmd *cobra.Command
func newPromptCmd() *cobra.Command {
promptModeCmd = &cobra.Command{
Use: "prompt",
Short: "enter the interactive gnmic prompt mode",
PreRunE: gApp.PromptPreRunE,
RunE: gApp.PromptRunE,
PostRun: func(cmd *cobra.Command, _ []string) {
cmd.ResetFlags()
//initPromptFlags(cmd)
},
SilenceUsage: true,
}
gApp.InitPromptFlags(promptModeCmd)
return promptModeCmd
}
var promptQuitCmd = &cobra.Command{
Use: "quit",
Short: "quit the gnmic-prompt",
Run: func(_ *cobra.Command, _ []string) {
// cancel gctx
gApp.Cfn()
// save history
home, err := homedir.Dir()
if err != nil {
os.Exit(0)
}
f, err := os.Create(home + "/.gnmic.history")
if err != nil {
os.Exit(0)
}
l := len(gApp.PromptHistory)
if l > 128 {
gApp.PromptHistory = gApp.PromptHistory[l-128:]
}
for i := range gApp.PromptHistory {
f.WriteString(gApp.PromptHistory[i] + "\n")
}
f.Close()
os.Exit(0)
},
}
var targetCmd = &cobra.Command{
Use: "target",
Short: "manipulate configured targets",
}
var targetListCmd = &cobra.Command{
Use: "list",
Short: "list configured targets",
RunE: func(_ *cobra.Command, _ []string) error {
targetsConfig, err := gApp.Config.GetTargets()
if err != nil {
return err
}
tabData := targetTable(targetsConfig, true)
renderTable(tabData, targetListHeader)
return nil
},
PostRun: func(_ *cobra.Command, _ []string) {
name = ""
},
}
var targetShowCmd = &cobra.Command{
Use: "show",
Short: "show a target details",
Annotations: map[string]string{
"--name": "TARGET",
},
RunE: func(_ *cobra.Command, _ []string) error {
if name == "" {
fmt.Println("provide a target name with --name")
return nil
}
targetsConfig, err := gApp.Config.GetTargets()
if err != nil {
return err
}
if tc, ok := targetsConfig[name]; ok {
tabData := targetTable(map[string]*types.TargetConfig{name: tc}, false)
renderTable(tabData, []string{"Param", "Value"})
return nil
}
return errors.New("unknown target")
},
PostRun: func(_ *cobra.Command, _ []string) {
name = ""
},
}
var subscriptionCmd = &cobra.Command{
Use: "subscription",
Short: "manipulate configured subscriptions",
}
var subscriptionListCmd = &cobra.Command{
Use: "list",
Short: "list configured subscriptions",
RunE: func(_ *cobra.Command, _ []string) error {
subs, err := gApp.Config.GetSubscriptions(nil)
if err != nil {
return err
}
tabData := subscriptionTable(subs, true)
renderTable(tabData, subscriptionListHeader)
return nil
},
PostRun: func(_ *cobra.Command, _ []string) {
name = ""
},
}
var subscriptionShowCmd = &cobra.Command{
Use: "show",
Short: "show a subscription details",
Annotations: map[string]string{
"--name": "SUBSCRIPTION",
},
RunE: func(_ *cobra.Command, _ []string) error {
if name == "" {
fmt.Println("provide a subscription name with --name")
return nil
}
subs, err := gApp.Config.GetSubscriptions(nil)
if err != nil {
return err
}
if s, ok := subs[name]; ok {
tabData := subscriptionTable(map[string]*types.SubscriptionConfig{name: s}, false)
renderTable(tabData, []string{"Param", "Value"})
return nil
}
return errors.New("unknown subscription")
},
PostRun: func(_ *cobra.Command, _ []string) {
name = ""
},
}
var outputCmd = &cobra.Command{
Use: "output",
Short: "manipulate configured outputs",
}
var outputListCmd = &cobra.Command{
Use: "list",
Short: "list configured outputs",
RunE: func(_ *cobra.Command, _ []string) error {
tabData := gApp.Config.GetOutputsConfigs()
renderTable(tabData, []string{"Name", "Config"})
return nil
},
}
func renderTable(tabData [][]string, header []string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.AppendBulk(tabData)
table.Render()
}
func targetTable(targetConfigs map[string]*types.TargetConfig, list bool) [][]string {
if list {
tabData := make([][]string, 0)
for _, tc := range targetConfigs {
tabData = append(tabData, []string{
tc.Name,
tc.Address,
tc.UsernameString(),
tc.PasswordString(),
tc.InsecureString(),
tc.SkipVerifyString(),
tc.TLSCAString(),
tc.TLSCertString(),
tc.TLSKeyString(),
})
}
sort.Slice(tabData, func(i, j int) bool {
return tabData[i][0] < tabData[j][0]
})
return tabData
}
if len(targetConfigs) > 1 {
gApp.Logger.Printf("cannot show multiple targets")
return nil
}
for _, tc := range targetConfigs {
tabData := make([][]string, 0, 16)
tabData = append(tabData, []string{"Name", tc.Name})
tabData = append(tabData, []string{"Address", tc.Address})
tabData = append(tabData, []string{"Username", tc.UsernameString()})
tabData = append(tabData, []string{"Password", tc.PasswordString()})
tabData = append(tabData, []string{"Insecure", tc.InsecureString()})
tabData = append(tabData, []string{"Skip Verify", tc.SkipVerifyString()})
tabData = append(tabData, []string{"TLS CA", tc.TLSCAString()})
tabData = append(tabData, []string{"TLS Certificate", tc.TLSCertString()})
tabData = append(tabData, []string{"TLS Key", tc.TLSKeyString()})
tabData = append(tabData, []string{"TLS Min Version", tc.TLSMinVersion})
tabData = append(tabData, []string{"TLS Max Version", tc.TLSMaxVersion})
tabData = append(tabData, []string{"TLS Version", tc.TLSVersion})
tabData = append(tabData, []string{"Subscriptions", strings.Join(tc.Subscriptions, "\n")})
tabData = append(tabData, []string{"Outputs", strings.Join(tc.Outputs, "\n")})
tabData = append(tabData, []string{"Buffer Size", tc.BufferSizeString()})
tabData = append(tabData, []string{"Retry Timer", tc.RetryTimer.String()})
return tabData
}
return [][]string{}
}
func subscriptionTable(scs map[string]*types.SubscriptionConfig, list bool) [][]string {
if list {
tabData := make([][]string, 0, len(scs))
for _, sub := range scs {
enc := ""
if sub.Encoding != nil {
enc = *sub.Encoding
}
tabData = append(tabData, []string{
sub.Name,
sub.ModeString(),
sub.PrefixString(),
sub.PathsString(),
sub.SampleIntervalString(),
enc,
})
}
sort.Slice(tabData, func(i, j int) bool {
return tabData[i][0] < tabData[j][0]
})
return tabData
}
if len(scs) > 1 {
gApp.Logger.Printf("cannot show multiple subscriptions")
return nil
}
for _, sub := range scs {
tabData := make([][]string, 0, 8)
tabData = append(tabData, []string{"Name", sub.Name})
tabData = append(tabData, []string{"Mode", sub.ModeString()})
tabData = append(tabData, []string{"Prefix", sub.PrefixString()})
tabData = append(tabData, []string{"Paths", sub.PathsString()})
tabData = append(tabData, []string{"Sample Interval", sub.SampleIntervalString()})
tabData = append(tabData, []string{"Encoding", *sub.Encoding})
tabData = append(tabData, []string{"Qos", sub.QosString()})
tabData = append(tabData, []string{"Heartbeat Interval", sub.HeartbeatIntervalString()})
return tabData
}
return [][]string{}
}
var name string
func findMatchedXPATH(entry *yang.Entry, input string, prefixPresent bool) []goprompt.Suggest {
if strings.HasPrefix(input, ":") {
return nil
}
suggestions := make([]goprompt.Suggest, 0, 4)
inputLen := len(input)
for i, c := range input {
if c == ':' && i+1 < inputLen {
input = input[i+1:]
inputLen -= (i + 1)
break
}
}
prependOrigin := gApp.Config.LocalFlags.PromptSuggestWithOrigin && !prefixPresent
for name, child := range entry.Dir {
if child.IsCase() || child.IsChoice() {
for _, gchild := range child.Dir {
suggestions = append(suggestions, findMatchedXPATH(gchild, input, prefixPresent)...)
}
continue
}
pathelem := "/" + name
if strings.HasPrefix(pathelem, input) {
node := ""
if inputLen == 0 && prependOrigin {
node = fmt.Sprintf("%s:/%s", entry.Name, name)
} else if inputLen > 0 && input[0] == '/' {
node = name
} else {
node = pathelem
}
suggestions = append(suggestions, goprompt.Suggest{Text: node, Description: buildXPATHDescription(child)})
if child.Key != "" { // list
keylist := strings.Split(child.Key, " ")
for _, key := range keylist {
node = fmt.Sprintf("%s[%s=*]", node, key)
}
suggestions = append(suggestions, goprompt.Suggest{Text: node, Description: buildXPATHDescription(child)})
}
} else if strings.HasPrefix(input, pathelem) {
var prevC rune
var bracketCount int
var endIndex int = -1
var stop bool
for i, c := range input {
switch c {
case '[':
bracketCount++
case ']':
if prevC != '\\' {
bracketCount--
endIndex = i
}
case '/':
if i != 0 && bracketCount == 0 {
endIndex = i
stop = true
}
}
if stop {
break
}
prevC = c
}
if bracketCount == 0 {
if endIndex >= 0 {
suggestions = append(suggestions, findMatchedXPATH(child, input[endIndex:], prefixPresent)...)
} else {
suggestions = append(suggestions, findMatchedXPATH(child, input[len(pathelem):], prefixPresent)...)
}
}
}
}
return suggestions
}
func getDescriptionPrefix(entry *yang.Entry) string {
switch {
case entry.Dir == nil && entry.ListAttr != nil: // leaf-list
return "[⋯]"
case entry.Dir == nil: // leaf
return " "
case entry.ListAttr != nil: // list
return "[+]"
default: // container
return "[+]"
}
}
func getEntryType(entry *yang.Entry) string {
if entry.Type != nil {
return entry.Type.Kind.String()
}
return ""
}
func buildXPATHDescription(entry *yang.Entry) string {
sb := strings.Builder{}
sb.WriteString(getDescriptionPrefix(entry))
sb.WriteString(" ")
sb.WriteString(getPermissions(entry))
sb.WriteString(" ")
if gApp.Config.LocalFlags.PromptDescriptionWithTypes {
n, _ := sb.WriteString(getEntryType(entry))
if n > 0 {
sb.WriteString(", ")
}
}
if gApp.Config.LocalFlags.PromptDescriptionWithPrefix {
if entry.Prefix != nil {
sb.WriteString(entry.Prefix.Name)
sb.WriteString(": ")
}
}
sb.WriteString(entry.Description)
return sb.String()
}
func getPermissions(entry *yang.Entry) string {
if entry == nil {
return "(rw)"
}
switch entry.Config {
case yang.TSTrue:
return "(rw)"
case yang.TSFalse:
return "(ro)"
case yang.TSUnset:
return getPermissions(entry.Parent)
}
return "(rw)"
}
func findMatchedSchema(entry *yang.Entry, input string) []*yang.Entry {
schemaNodes := []*yang.Entry{}
for name, child := range entry.Dir {
pathelem := "/" + name
if strings.HasPrefix(pathelem, input) {
schemaNodes = append(schemaNodes, child)
if child.Key != "" { // list
schemaNodes = append(schemaNodes, child)
}
} else if strings.HasPrefix(input, pathelem) {
var prevC rune
var bracketCount int
var endIndex int = -1
var stop bool
for i, c := range input {
switch c {
case '[':
bracketCount++
case ']':
if prevC != '\\' {
bracketCount--
endIndex = i
}
case '/':
if i != 0 && bracketCount == 0 {
endIndex = i
stop = true
}
}
if stop {
break
}
prevC = c
}
if bracketCount == 0 {
if endIndex >= 0 {
schemaNodes = append(schemaNodes, findMatchedSchema(child, input[endIndex:])...)
} else {
schemaNodes = append(schemaNodes, findMatchedSchema(child, input[len(pathelem):])...)
}
}
}
}
return schemaNodes
}
var filePathCompleter = completer.FilePathCompleter{
IgnoreCase: true,
Filter: func(fi os.FileInfo) bool {
return fi.IsDir() || !strings.HasPrefix(fi.Name(), ".")
},
}
var yangPathCompleter = completer.FilePathCompleter{
IgnoreCase: true,
Filter: func(fi os.FileInfo) bool {
return fi.IsDir() || strings.HasSuffix(fi.Name(), ".yang")
},
}
var dirPathCompleter = completer.FilePathCompleter{
IgnoreCase: true,
Filter: func(fi os.FileInfo) bool {
return fi.IsDir()
},
}
func findDynamicSuggestions(annotation string, doc goprompt.Document) []goprompt.Suggest {
switch annotation {
case "XPATH":
line := doc.CurrentLine()
word := doc.GetWordBeforeCursor()
suggestions := make([]goprompt.Suggest, 0, 16)
entries := []*yang.Entry{}
if index := strings.Index(line, "--prefix"); index >= 0 {
line = strings.TrimLeft(line[index+8:], " ") // 8 is len("--prefix")
end := strings.Index(line, " ")
if end >= 0 {
line = line[:end]
lineLen := len(line)
// remove "origin:" from prefix if present
for i, c := range line {
if c == ':' && i+1 < lineLen {
line = line[i+1:]
break
}
}
// find yang entries matching the prefix
for _, entry := range gApp.SchemaTree.Dir {
entries = append(entries, findMatchedSchema(entry, line)...)
}
// generate suggestions from matching entries
for _, entry := range entries {
suggestions = append(suggestions, findMatchedXPATH(entry, word, true)...)
}
}
} else {
// generate suggestions from yang schema
for _, entry := range gApp.SchemaTree.Dir {
suggestions = append(suggestions, findMatchedXPATH(entry, word, false)...)
}
}
sort.Slice(suggestions, func(i, j int) bool {
if suggestions[i].Text == suggestions[j].Text {
return suggestions[i].Description < suggestions[j].Description
}
return suggestions[i].Text < suggestions[j].Text
})
return suggestions
case "PREFIX":
word := doc.GetWordBeforeCursor()
suggestions := make([]goprompt.Suggest, 0, 16)
for _, entry := range gApp.SchemaTree.Dir {
suggestions = append(suggestions, findMatchedXPATH(entry, word, false)...)
}
sort.Slice(suggestions, func(i, j int) bool {
if suggestions[i].Text == suggestions[j].Text {
return suggestions[i].Description < suggestions[j].Description
}
return suggestions[i].Text < suggestions[j].Text
})
return suggestions
case "FILE":
return filePathCompleter.Complete(doc)
case "YANG":
return yangPathCompleter.Complete(doc)
case "MODEL":
suggestions := make([]goprompt.Suggest, 0, len(gApp.SchemaTree.Dir))
for name, dir := range gApp.SchemaTree.Dir {
if dir != nil {
suggestions = append(suggestions, goprompt.Suggest{Text: name, Description: dir.Description})
continue
}
suggestions = append(suggestions, goprompt.Suggest{Text: name})
}
sort.Slice(suggestions, func(i, j int) bool {
if suggestions[i].Text == suggestions[j].Text {
return suggestions[i].Description < suggestions[j].Description
}
return suggestions[i].Text < suggestions[j].Text
})
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "DIR":
return dirPathCompleter.Complete(doc)
case "ENCODING":
suggestions := make([]goprompt.Suggest, 0, len(encodings))
for _, sugg := range encodings {
suggestions = append(suggestions, goprompt.Suggest{Text: sugg[0], Description: sugg[1]})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "FORMAT":
suggestions := make([]goprompt.Suggest, 0, len(formats))
for _, sugg := range formats {
suggestions = append(suggestions, goprompt.Suggest{Text: sugg[0], Description: sugg[1]})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "STORE":
suggestions := make([]goprompt.Suggest, 0, len(get.DataType))
for _, sugg := range get.DataType {
suggestions = append(suggestions, goprompt.Suggest{Text: sugg[0], Description: sugg[1]})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "SUBSC_MODE":
suggestions := make([]goprompt.Suggest, 0, len(subscribe.Modes))
for _, sugg := range subscribe.Modes {
suggestions = append(suggestions, goprompt.Suggest{Text: sugg[0], Description: sugg[1]})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "STREAM_MODE":
suggestions := make([]goprompt.Suggest, 0, len(subscribe.StreamModes))
for _, sugg := range subscribe.StreamModes {
suggestions = append(suggestions, goprompt.Suggest{Text: sugg[0], Description: sugg[1]})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "SUBSCRIPTION":
subs := gApp.Config.GetSubscriptionsFromFile()
suggestions := make([]goprompt.Suggest, 0, len(subs))
for _, sub := range subs {
suggestions = append(suggestions, goprompt.Suggest{Text: sub.Name, Description: subscriptionDescription(sub)})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "TARGET":
targetsConfig := gApp.Config.TargetsList()
suggestions := make([]goprompt.Suggest, 0, len(targetsConfig))
for _, target := range targetsConfig {
sb := strings.Builder{}
if target.Name != target.Address {
sb.WriteString("address=")
sb.WriteString(target.Address)
sb.WriteString(", ")
}
sb.WriteString("secure=")
if *target.Insecure {
sb.WriteString("false")
} else {
sb.WriteString(fmt.Sprintf("%v", !(strings.Contains(doc.CurrentLine(), "--insecure"))))
}
suggestions = append(suggestions, goprompt.Suggest{Text: target.Name, Description: sb.String()})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
case "OUTPUT":
outputGroups := gApp.Config.GetOutputsSuggestions()
suggestions := make([]goprompt.Suggest, 0, len(outputGroups))
for _, sugg := range outputGroups {
suggestions = append(suggestions, goprompt.Suggest{Text: sugg.Name, Description: strings.Join(sugg.Types, ", ")})
}
return goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
}
return []goprompt.Suggest{}
}
func subscriptionDescription(sub *types.SubscriptionConfig) string {
sb := strings.Builder{}
sb.WriteString("mode=")
sb.WriteString(sub.Mode)
sb.WriteString(", ")
if strings.ToLower(sub.Mode) == "stream" {
sb.WriteString("stream-mode=")
sb.WriteString(sub.StreamMode)
sb.WriteString(", ")
if strings.ToLower(sub.StreamMode) == "sample" {
sb.WriteString("sample-interval=")
sb.WriteString(sub.SampleInterval.String())
sb.WriteString(", ")
}
}
if sub.Encoding != nil {
sb.WriteString("encoding=")
sb.WriteString(*sub.Encoding)
sb.WriteString(", ")
}
if sub.Prefix != "" {
sb.WriteString("prefix=")
sb.WriteString(sub.Prefix)
sb.WriteString(", ")
}
sb.WriteString("path(s)=")
sb.WriteString(strings.Join(sub.Paths, ","))
return sb.String()
}
func showCommandArguments(b *goprompt.Buffer) {
doc := b.Document()
showLocalFlags := false
command := gApp.RootCmd
args := strings.Fields(doc.CurrentLine())
if found, _, err := command.Find(args); err == nil {
if command != found {
showLocalFlags = true
}
command = found
}
maxNameLen := 0
suggestions := make([]goprompt.Suggest, 0, 32)
if command.HasAvailableSubCommands() {
for _, c := range command.Commands() {
if c.Hidden {
continue
}
length := len(c.Name())
if maxNameLen < length {
maxNameLen = length
}
suggestions = append(suggestions, goprompt.Suggest{Text: c.Name(), Description: c.Short})
}
}
if showLocalFlags {
addFlags := func(flag *pflag.Flag) {
if flag.Hidden {
return
}
length := len(flag.Name)
if maxNameLen < length+2 {
maxNameLen = length + 2
}
suggestions = append(suggestions, goprompt.Suggest{Text: "--" + flag.Name, Description: flag.Usage})
}
command.LocalFlags().VisitAll(addFlags)
}
suggestions = goprompt.FilterHasPrefix(suggestions, doc.GetWordBeforeCursor(), true)
if len(suggestions) == 0 {
return
}
if err := termbox.Init(); err != nil {
gApp.Logger.Fatalf("%v", err)
}
w, _ := termbox.Size()
termbox.Close()
fmt.Printf("\n")
maxDescLen := w - maxNameLen - 6
format := fmt.Sprintf(" %%-%ds : %%-%ds\n", maxNameLen, maxDescLen)
for i := range suggestions {
length := len(suggestions[i].Description)
if length > maxDescLen {
fmt.Printf(format, suggestions[i].Text, suggestions[i].Description[:maxDescLen])
} else {
fmt.Printf(format, suggestions[i].Text, suggestions[i].Description)
}
}
fmt.Printf("\n")
}
// ExecutePrompt load and run gnmic-prompt mode.
func ExecutePrompt() {
initPromptCmds()
shell := &cmdPrompt{
RootCmd: gApp.RootCmd,
GoPromptOptions: []goprompt.Option{
goprompt.OptionTitle("gnmic-prompt"),
goprompt.OptionPrefix("gnmic> "),
goprompt.OptionHistory(gApp.PromptHistory),
goprompt.OptionMaxSuggestion(gApp.Config.LocalFlags.PromptMaxSuggestions),
goprompt.OptionPrefixTextColor(getColor("prefix-color")),
goprompt.OptionPreviewSuggestionTextColor(goprompt.Cyan),
goprompt.OptionSuggestionTextColor(goprompt.White),
goprompt.OptionSuggestionBGColor(getColor("suggestions-bg-color")),
goprompt.OptionSelectedSuggestionTextColor(goprompt.Black),
goprompt.OptionSelectedSuggestionBGColor(goprompt.White),
goprompt.OptionDescriptionTextColor(goprompt.LightGray),
goprompt.OptionDescriptionBGColor(getColor("description-bg-color")),
goprompt.OptionSelectedDescriptionTextColor(goprompt.Black),
goprompt.OptionSelectedDescriptionBGColor(goprompt.White),
goprompt.OptionScrollbarBGColor(goprompt.DarkGray),
goprompt.OptionScrollbarThumbColor(goprompt.Blue),
goprompt.OptionAddASCIICodeBind(
// bind '?' character to show cmd args
goprompt.ASCIICodeBind{
ASCIICode: []byte{0x3f},
Fn: showCommandArguments,
},
// bind OS X Option+Left key binding
goprompt.ASCIICodeBind{
ASCIICode: []byte{0x1b, 0x62},
Fn: goprompt.GoLeftWord,
},
// bind OS X Option+Right key binding
goprompt.ASCIICodeBind{
ASCIICode: []byte{0x1b, 0x66},
Fn: goprompt.GoRightWord,
},
),
goprompt.OptionAddKeyBind(
// bind Linux CTRL+Left key binding
goprompt.KeyBind{
Key: goprompt.ControlLeft,
Fn: goprompt.GoLeftWord,
},
// bind Linux CTRL+Right key binding
goprompt.KeyBind{
Key: goprompt.ControlRight,
Fn: goprompt.GoRightWord,
},
// bind CTRL+Z key to delete path elements
goprompt.KeyBind{
Key: goprompt.ControlZ,
Fn: func(buf *goprompt.Buffer) {
// If the last word before the cursor does not contain a "/" return.
// This is needed to avoid deleting down to a previous flag value
if !strings.Contains(buf.Document().GetWordBeforeCursorWithSpace(), "/") {
return
}
// Check if the last rune is a PathSeparator and is not the path root then delete it
if buf.Document().GetCharRelativeToCursor(0) == os.PathSeparator && buf.Document().GetCharRelativeToCursor(-1) != ' ' {
buf.DeleteBeforeCursor(1)
}
// Delete down until the next "/"
buf.DeleteBeforeCursor(len([]rune(buf.Document().GetWordBeforeCursorUntilSeparator("/"))))
},
},
),
goprompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator),
// goprompt.OptionCompletionOnDown(),
goprompt.OptionShowCompletionAtStart(),
},
}
shell.Run()
}
func initPromptCmds() {
gApp.RootCmd.AddCommand(promptQuitCmd)
gApp.RootCmd.AddCommand(targetCmd)
gApp.RootCmd.AddCommand(subscriptionCmd)
gApp.RootCmd.AddCommand(outputCmd)
targetCmd.AddCommand(targetListCmd)
targetCmd.AddCommand(targetShowCmd)
targetShowCmd.Flags().StringVarP(&name, "name", "", "", "target name")
subscriptionCmd.AddCommand(subscriptionListCmd)
subscriptionCmd.AddCommand(subscriptionShowCmd)
subscriptionShowCmd.Flags().StringVarP(&name, "name", "", "", "subscription name")
outputCmd.AddCommand(outputListCmd)
gApp.RootCmd.RemoveCommand(promptModeCmd)
}
// Reference: https://github.com/stromland/cobra-prompt
// cmdPrompt requires RootCmd to run
type cmdPrompt struct {
// RootCmd is the start point, all its sub commands and flags will be available as suggestions
RootCmd *cobra.Command
// GoPromptOptions is for customize go-prompt
// see https://github.com/c-bata/go-prompt/blob/master/option.go
GoPromptOptions []goprompt.Option
}
// Run will automatically generate suggestions for all cobra commands
// and flags defined by RootCmd and execute the selected commands.
func (co cmdPrompt) Run() {
p := goprompt.New(
func(in string) {
promptArgs, err := parsePromptArgs(in)
if err != nil {
fmt.Fprint(os.Stderr, err)
return
}
os.Args = append([]string{os.Args[0]}, promptArgs...)
if len(promptArgs) > 0 {
err := co.RootCmd.Execute()
if err == nil && in != "" {
gApp.PromptHistory = append(gApp.PromptHistory, in)
}
}
},
func(d goprompt.Document) []goprompt.Suggest {
return findSuggestions(co, d)
},
co.GoPromptOptions...,
)
p.Run()
}
func parsePromptArgs(in string) ([]string, error) {
var m = []string{}
var s string
// space suffix ensures the last string is appended
in = strings.TrimSpace(in) + " "
lastQuote := rune(0)
isSpace := false
for _, c := range in {
switch {
// ending a quoted item, break out, skip this character and reset lastQuote
case c == lastQuote:
lastQuote = rune(0)
// in a quoted item, include this character
case lastQuote != rune(0):
s += string(c)
// starting a quoted item, set lastQuote
case unicode.In(c, unicode.Quotation_Mark):
isSpace = false
lastQuote = c
// a space, append the string to the list
// if it was not already added (previous char was a space)
// and reset string s
case unicode.IsSpace(c):
if isSpace {
continue
}
isSpace = true
m = append(m, s)
s = ""
// add the char to the string
default:
isSpace = false
s += string(c)
}
}
if lastQuote != rune(0) {
return nil, fmt.Errorf("quotes not closed")
}
return m, nil
}
func findSuggestions(co cmdPrompt, doc goprompt.Document) []goprompt.Suggest {
command := co.RootCmd
args := strings.Fields(doc.CurrentLine())
if found, _, err := command.Find(args); err == nil {
command = found
}
suggestions := make([]goprompt.Suggest, 0, 32)
// check flag annotation for the dynamic suggestion
annotation := ""
argnum := len(args)
wordBefore := doc.GetWordBeforeCursor()
if wordBefore == "" {
if argnum >= 1 {
annotation = command.Annotations[args[argnum-1]]
}
} else {
if argnum >= 2 {
annotation = command.Annotations[args[argnum-2]]
}
}
if annotation != "" {
return append(suggestions, findDynamicSuggestions(annotation, doc)...)
}
// add sub commands suggestions if they exist
if command.HasAvailableSubCommands() {
for _, c := range command.Commands() {
if !c.Hidden {
suggestions = append(suggestions, goprompt.Suggest{Text: c.Name(), Description: c.Short})
}
}
}
addFlags := func(flag *pflag.Flag) {
if flag.Hidden {
return
}
suggestions = append(suggestions, goprompt.Suggest{Text: "--" + flag.Name, Description: flag.Usage})
}
// load local flags
command.LocalFlags().VisitAll(addFlags)