-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathcompletion.go
1853 lines (1447 loc) · 55.8 KB
/
completion.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
package main
import (
"io/fs"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/cobra"
"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
)
// cmpClusterGroupNames provides shell completion for cluster group names.
// It takes a partial input string and returns a list of matching names along with a shell completion directive.
func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
cluster, _, err := resource.server.GetCluster()
if err != nil || !cluster.Enabled {
return nil, cobra.ShellCompDirectiveError
}
results, err := resource.server.GetClusterGroupNames()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return results, cmpDirectives
}
// cmpClusterGroups provides shell completion for cluster groups and their remotes.
// It takes a partial input string and returns a list of matching cluster groups along with a shell completion directive.
func (g *cmdGlobal) cmpClusterGroups(toComplete string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
cluster, _, err := resource.server.GetCluster()
if err != nil || !cluster.Enabled {
return nil, cobra.ShellCompDirectiveError
}
groups, err := resource.server.GetClusterGroupNames()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results := make([]string, 0, len(groups))
for _, group := range groups {
var name string
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = group
} else {
name = resource.remote + ":" + group
}
results = append(results, name)
}
if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
return results, cmpDirectives
}
// cmpClusterMemberAllConfigKeys provides shell completion for all cluster member configuration keys.
// It takes a partial input string and returns a list of all cluster member configuration keys along with a shell completion directive.
func (g *cmdGlobal) cmpClusterMemberAllConfigKeys(memberName string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
// Parse remote
resources, err := g.ParseServers(memberName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
cluster, _, err := client.GetCluster()
if err != nil || !cluster.Enabled {
return nil, cobra.ShellCompDirectiveError
}
metadataConfiguration, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
clusterConfig, ok := metadataConfiguration.Configs["cluster"]
if !ok {
return nil, cobra.ShellCompDirectiveError
}
// Pre-allocate configKeys slice capacity.
keyCount := 0
for _, field := range clusterConfig {
keyCount += len(field.Keys)
}
configKeys := make([]string, 0, keyCount)
for _, field := range clusterConfig {
for _, key := range field.Keys {
for configKey := range key {
configKeys = append(configKeys, configKey)
}
}
}
return configKeys, cmpDirectives
}
// cmpClusterMemberConfigs provides shell completion for cluster member configs.
// It takes a partial input string (member name) and returns a list of matching cluster member configs along with a shell completion directive.
func (g *cmdGlobal) cmpClusterMemberConfigs(memberName string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(memberName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
cluster, _, err := client.GetCluster()
if err != nil || !cluster.Enabled {
return nil, cobra.ShellCompDirectiveError
}
member, _, err := client.GetClusterMember(memberName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results := make([]string, 0, len(member.Config))
for k := range member.Config {
results = append(results, k)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// cmpClusterMemberRoles provides shell completion for cluster member roles.
// It takes a member name and returns a list of matching cluster member roles along with a shell completion directive.
func (g *cmdGlobal) cmpClusterMemberRoles(memberName string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(memberName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
cluster, _, err := client.GetCluster()
if err != nil || !cluster.Enabled {
return nil, cobra.ShellCompDirectiveError
}
member, _, err := client.GetClusterMember(memberName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return member.Roles, cobra.ShellCompDirectiveNoFileComp
}
// cmpClusterMembers provides shell completion for cluster members.
// It takes a partial input string and returns a list of matching cluster members along with a shell completion directive.
func (g *cmdGlobal) cmpClusterMembers(toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
if len(resources) > 0 {
resource := resources[0]
cluster, _, err := resource.server.GetCluster()
if err != nil || !cluster.Enabled {
return nil, cobra.ShellCompDirectiveError
}
// Get the cluster members
members, err := resource.server.GetClusterMembers()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results = make([]string, 0, len(members))
for _, member := range members {
var name string
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = member.ServerName
} else {
name = resource.remote + ":" + member.ServerName
}
results = append(results, name)
}
}
if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
return results, cmpDirectives
}
// cmpImages provides shell completion for image aliases.
// It takes a partial input string and returns a list of matching image aliases along with a shell completion directive.
func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
remote, _, found := strings.Cut(toComplete, ":")
if !found {
remote = g.conf.DefaultRemote
}
remoteServer, _ := g.conf.GetImageServer(remote)
images, _ := remoteServer.GetImages()
results := make([]string, 0, len(images))
for _, image := range images {
for _, alias := range image.Aliases {
var name string
if remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = alias.Name
} else {
name = remote + ":" + alias.Name
}
results = append(results, name)
}
}
if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(toComplete, true)
results = append(results, remotes...)
cmpDirectives |= directives
}
return results, cmpDirectives
}
// cmpImageFingerprintsFromRemote provides shell completion for image fingerprints.
// It takes a partial input string and a remote and returns image fingerprints for that remote along with a shell completion directive.
func (g *cmdGlobal) cmpImageFingerprintsFromRemote(toComplete string, remote string) ([]string, cobra.ShellCompDirective) {
if remote == "" {
remote = g.conf.DefaultRemote
}
remoteServer, _ := g.conf.GetImageServer(remote)
images, _ := remoteServer.GetImages()
results := make([]string, 0, len(images))
for _, image := range images {
if !strings.HasPrefix(image.Fingerprint, toComplete) {
continue
}
results = append(results, image.Fingerprint)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// cmpInstanceKeys provides shell completion for all instance configuration keys.
// It takes an instance name to determine instance type and returns a list of all instance configuration keys along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceKeys(instanceName string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
// Early return when completing server keys.
_, instanceNameOnly, found := strings.Cut(instanceName, ":")
if instanceNameOnly == "" && found {
return g.cmpServerAllKeys(instanceName)
}
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
instance, _, err := client.GetInstance(instanceName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
// Complete keys based on instance type.
instanceType := instance.Type
metadataConfiguration, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
instanceConfig, ok := metadataConfiguration.Configs["instance"]
if !ok {
return nil, cobra.ShellCompDirectiveError
}
instanceTypeAnyKeys := make([]string, 0, len(instancetype.InstanceConfigKeysAny))
for key := range instancetype.InstanceConfigKeysAny {
instanceTypeAnyKeys = append(instanceTypeAnyKeys, key)
}
instanceTypeContainerKeys := make([]string, 0, len(instancetype.InstanceConfigKeysContainer))
for key := range instancetype.InstanceConfigKeysContainer {
instanceTypeContainerKeys = append(instanceTypeContainerKeys, key)
}
instanceTypeVMKeys := make([]string, 0, len(instancetype.InstanceConfigKeysVM))
for key := range instancetype.InstanceConfigKeysVM {
instanceTypeVMKeys = append(instanceTypeVMKeys, key)
}
// Pre-allocate configKeys slice capacity.
keyCount := 0
for _, field := range instanceConfig {
keyCount += len(field.Keys)
}
configKeys := make([]string, 0, keyCount)
for _, field := range instanceConfig {
for _, key := range field.Keys {
for configKey := range key {
configKey = strings.TrimSuffix(configKey, "*")
if shared.ValueInSlice(configKey, instanceTypeAnyKeys) || shared.StringHasPrefix(configKey, instancetype.ConfigKeyPrefixesAny...) {
configKeys = append(configKeys, configKey)
}
if instanceType == string(api.InstanceTypeContainer) && (shared.ValueInSlice(configKey, instanceTypeContainerKeys) || shared.StringHasPrefix(configKey, instancetype.ConfigKeyPrefixesContainer...)) {
configKeys = append(configKeys, configKey)
} else if instanceType == string(api.InstanceTypeVM) && shared.ValueInSlice(configKey, instanceTypeVMKeys) {
configKeys = append(configKeys, configKey)
}
}
}
}
return configKeys, cmpDirectives | cobra.ShellCompDirectiveNoSpace
}
// cmpInstanceAllKeys provides shell completion for all possible instance configuration keys.
// It returns a list of all possible instance configuration keys along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceAllKeys(profileName string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
// Parse remote
resources, err := g.ParseServers(profileName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
metadataConfiguration, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
instanceConfig, ok := metadataConfiguration.Configs["instance"]
if !ok {
return nil, cobra.ShellCompDirectiveError
}
// Pre-allocate configKeys slice capacity.
keyCount := 0
for _, field := range instanceConfig {
keyCount += len(field.Keys)
}
configKeys := make([]string, 0, keyCount)
for _, field := range instanceConfig {
for _, key := range field.Keys {
for configKey := range key {
configKey = strings.TrimSuffix(configKey, "*")
configKeys = append(configKeys, configKey)
}
}
}
return configKeys, cmpDirectives | cobra.ShellCompDirectiveNoSpace
}
// cmpInstanceSetKeys provides shell completion for instance configuration keys which are currently set.
// It takes an instance name to determine instance type and returns a list of instance configuration keys along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceSetKeys(instanceName string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
// Early return when completing server keys.
_, instanceNameOnly, found := strings.Cut(instanceName, ":")
if instanceNameOnly == "" && found {
return g.cmpServerSetKeys(instanceName)
}
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
instance, _, err := client.GetInstance(instanceName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
// Pre-allocate configKeys slice capacity.
keyCount := len(instance.Config)
configKeys := make([]string, 0, keyCount)
for configKey := range instance.Config {
if !shared.StringHasPrefix(configKey, []string{"volatile", "image"}...) {
configKeys = append(configKeys, configKey)
}
}
return configKeys, cmpDirectives | cobra.ShellCompDirectiveNoSpace
}
// cmpServerAllKeys provides shell completion for all server configuration keys.
// It takes a partial input string and returns a list of all server configuration keys along with a shell completion directive.
func (g *cmdGlobal) cmpServerAllKeys(toComplete string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(toComplete)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
metadataConfiguration, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
server, ok := metadataConfiguration.Configs["server"]
if !ok {
return nil, cobra.ShellCompDirectiveError
}
keyCount := 0
for _, field := range server {
keyCount += len(field.Keys)
}
keys := make([]string, 0, keyCount)
for _, field := range server {
for _, keyMap := range field.Keys {
for key := range keyMap {
keys = append(keys, key)
}
}
}
return keys, cobra.ShellCompDirectiveNoFileComp
}
// cmpServerSetKeys provides shell completion for server configuration keys which are currently set.
// It takes a partial input string and returns a list of server configuration keys along with a shell completion directive.
func (g *cmdGlobal) cmpServerSetKeys(toComplete string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, err := g.ParseServers(toComplete)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
server, _, err := resource.server.GetServer()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
// Fetch all server config keys that can be set.
allServerConfigKeys, _ := g.cmpServerAllKeys(resource.remote)
// Convert slice to map[string]struct{} for O(1) lookups.
keySet := make(map[string]struct{}, len(allServerConfigKeys))
for _, key := range allServerConfigKeys {
keySet[key] = struct{}{}
}
// Pre-allocate configKeys slice capacity.
keyCount := len(allServerConfigKeys)
configKeys := make([]string, 0, keyCount)
for configKey := range server.Config {
// We only want to return the intersection between allServerConfigKeys and configKeys to avoid returning the full server config.
_, exists := keySet[configKey]
if exists {
configKeys = append(configKeys, configKey)
}
}
return configKeys, cmpDirectives | cobra.ShellCompDirectiveNoSpace
}
// cmpInstanceConfigTemplates provides shell completion for instance config templates.
// It takes an instance name and returns a list of instance config templates along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceConfigTemplates(instanceName string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
_, instanceNameOnly, _ := strings.Cut(instanceName, ":")
if instanceNameOnly == "" {
instanceNameOnly = instanceName
}
results, err := client.GetInstanceTemplateFiles(instanceNameOnly)
if err != nil {
cobra.CompDebug(err.Error(), true)
return nil, cobra.ShellCompDirectiveError
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// cmpInstanceDeviceNames provides shell completion for instance devices.
// It takes an instance name and returns a list of instance device names along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceDeviceNames(instanceName string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
instanceNameOnly, _, err := client.GetInstance(instanceName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results := make([]string, 0, len(instanceNameOnly.Devices))
for k := range instanceNameOnly.Devices {
results = append(results, k)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// cmpInstanceAllDevices provides shell completion for all instance devices.
// It takes an instance name and returns a list of all possible instance devices along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceAllDevices(instanceName string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
metadataConfiguration, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
devices := make([]string, 0, len(metadataConfiguration.Configs))
for key := range metadataConfiguration.Configs {
if strings.HasPrefix(key, "device-") {
parts := strings.Split(key, "-")
deviceName := parts[1]
devices = append(devices, deviceName)
}
}
return devices, cobra.ShellCompDirectiveNoFileComp
}
// cmpInstanceAllDeviceOptions provides shell completion for all instance device options.
// It takes an instance name and device name and returns a list of all possible instance device options along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceAllDeviceOptions(instanceName string, deviceName string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
metadataConfiguration, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
deviceOptions := make([]string, 0, len(metadataConfiguration.Configs))
for key, device := range metadataConfiguration.Configs {
parts := strings.Split(key, "-")
if strings.HasPrefix(key, "device-") && parts[1] == deviceName {
conf := device["device-conf"]
for _, keyMap := range conf.Keys {
for option := range keyMap {
deviceOptions = append(deviceOptions, option)
}
}
}
}
return deviceOptions, cobra.ShellCompDirectiveNoFileComp
}
// cmpInstances provides shell completion for all instances.
// It takes a partial input string and returns a list of matching instances along with a shell completion directive.
func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
if len(resources) > 0 {
resource := resources[0]
instances, _ := resource.server.GetInstanceNames("")
results = make([]string, 0, len(instances))
for _, instance := range instances {
var name string
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = instance
} else {
name = resource.remote + ":" + instance
}
if !strings.HasPrefix(name, toComplete) {
continue
}
results = append(results, name)
}
}
if !strings.Contains(toComplete, ":") {
remotes, _ := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
}
return results, cmpDirectives
}
// cmpInstancesAction provides shell completion for all instance actions (start, pause, exec, stop and delete).
// It takes a partial input string, an action, and a boolean indicating if the force flag has been passed in. It returns a list of applicable instances based on their state and the requested action, along with a shell completion directive.
func (g *cmdGlobal) cmpInstancesAction(toComplete string, action string, flagForce bool) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
var filteredInstanceStatuses []string
switch action {
case "start":
filteredInstanceStatuses = append(filteredInstanceStatuses, "Stopped", "Frozen")
case "pause", "exec":
filteredInstanceStatuses = append(filteredInstanceStatuses, "Running")
case "stop":
if flagForce {
filteredInstanceStatuses = append(filteredInstanceStatuses, "Running", "Frozen")
} else {
filteredInstanceStatuses = append(filteredInstanceStatuses, "Running")
}
case "delete":
if flagForce {
filteredInstanceStatuses = append(filteredInstanceStatuses, api.GetAllStatusCodeStrings()...)
} else {
filteredInstanceStatuses = append(filteredInstanceStatuses, "Stopped")
}
default:
filteredInstanceStatuses = append(filteredInstanceStatuses, api.GetAllStatusCodeStrings()...)
}
if len(resources) > 0 {
resource := resources[0]
instances, _ := resource.server.GetInstances("")
results = make([]string, 0, len(instances))
for _, instance := range instances {
var name string
if shared.ValueInSlice(instance.Status, filteredInstanceStatuses) {
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = instance.Name
} else {
name = resource.remote + ":" + instance.Name
}
results = append(results, name)
}
}
if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
}
return results, cmpDirectives
}
// cmpInstancesAndSnapshots provides shell completion for instances and their snapshots.
// It takes a partial input string and returns a list of matching instances and their snapshots, along with a shell completion directive.
func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra.ShellCompDirective) {
results := []string{}
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
if len(resources) > 0 {
resource := resources[0]
if strings.Contains(resource.name, shared.SnapshotDelimiter) {
instName, _, _ := strings.Cut(resource.name, shared.SnapshotDelimiter)
snapshots, _ := resource.server.GetInstanceSnapshotNames(instName)
for _, snapshot := range snapshots {
results = append(results, instName+"/"+snapshot)
}
} else {
instances, _ := resource.server.GetInstanceNames("")
for _, instance := range instances {
var name string
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = instance
} else {
name = resource.remote + ":" + instance
}
results = append(results, name)
}
}
}
if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
return results, cmpDirectives
}
// cmpInstanceNamesFromRemote provides shell completion for instances for a specific remote.
// It takes a partial input string and returns a list of matching instances along with a shell completion directive.
func (g *cmdGlobal) cmpInstanceNamesFromRemote(toComplete string) ([]string, cobra.ShellCompDirective) {
resources, _ := g.ParseServers(toComplete)
if len(resources) > 0 {
resource := resources[0]
containers, _ := resource.server.GetInstanceNames("container")
vms, _ := resource.server.GetInstanceNames("virtual-machine")
results := append(containers, vms...)
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// cmpNetworkACLConfigs provides shell completion for network ACL configs.
// It takes an ACL name and returns a list of network ACL configs along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkACLConfigs(aclName string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(aclName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
acl, _, err := client.GetNetworkACL(resource.name)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results := make([]string, 0, len(acl.Config))
for k := range acl.Config {
results = append(results, k)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// cmpNetworkACLs provides shell completion for network ACL's.
// It takes a partial input string and returns a list of matching network ACL's along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(toComplete)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
acls, err := resource.server.GetNetworkACLNames()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results := make([]string, 0, len(acls))
for _, acl := range acls {
var name string
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = acl
} else {
name = resource.remote + ":" + acl
}
results = append(results, name)
}
if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
return results, cmpDirectives
}
// cmpNetworkACLRuleProperties provides shell completion for network ACL rule properties.
// It returns a list of network ACL rules provided by `networkACLRuleJSONStructFieldMap()“ along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkACLRuleProperties() ([]string, cobra.ShellCompDirective) {
allowedKeys := networkACLRuleJSONStructFieldMap()
results := make([]string, 0, len(allowedKeys))
for key := range allowedKeys {
results = append(results, key+"=")
}
return results, cobra.ShellCompDirectiveNoSpace
}
// cmpNetworkForwardConfigs provides shell completion for network forward configs.
// It takes a network name and listen address, and returns a list of network forward configs along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkForwardConfigs(networkName string, listenAddress string) ([]string, cobra.ShellCompDirective) {
// Parse remote
resources, err := g.ParseServers(networkName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
client := resource.server
forward, _, err := client.GetNetworkForward(networkName, listenAddress)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
results := make([]string, 0, len(forward.Config))
for k := range forward.Config {
results = append(results, k)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// cmpNetworkForwards provides shell completion for network forwards.
// It takes a network name and returns a list of network forwards along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkForwards(networkName string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(networkName)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
results, err := resource.server.GetNetworkForwardAddresses(networkName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return results, cmpDirectives
}
// cmpNetworkForwardPortTargetAddresses provides shell completion for network forward port target addresses.
// It takes a network name and listen address to determine whether to return ipv4 or ipv6 target addresses and returns a list of target addresses.
func (g *cmdGlobal) cmpNetworkForwardPortTargetAddresses(networkName string, listenAddress string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(networkName)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
instances, err := resource.server.GetInstancesFull(api.InstanceTypeAny)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var results []string
listenAddressIsIP4 := net.ParseIP(listenAddress).To4() != nil
for _, instance := range instances {
if instance.IsActive() && instance.State != nil && instance.State.Network != nil {
for _, network := range instance.State.Network {
if network.Type == "loopback" {
continue
}
results = make([]string, 0, len(network.Addresses))
for _, address := range network.Addresses {
if shared.ValueInSlice(address.Scope, []string{"link", "local"}) {
continue
}
if (listenAddressIsIP4 && address.Family == "inet") || (!listenAddressIsIP4 && address.Family == "inet6") {
results = append(results, address.Address)
}
}
}
}
}
return results, cmpDirectives
}
// cmpNetworkLoadBalancers provides shell completion for network load balancers.
// It takes a network name and returns a list of network load balancers along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkLoadBalancers(networkName string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
resources, _ := g.ParseServers(networkName)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}
resource := resources[0]
results, err := resource.server.GetNetworkLoadBalancerAddresses(networkName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return results, cmpDirectives
}