-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompletion.go
More file actions
1005 lines (900 loc) · 35.5 KB
/
completion.go
File metadata and controls
1005 lines (900 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
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 completion provides types and functions for command completion in the CLI
package completion
import (
"fmt"
"slices"
"strconv"
"strings"
"github.com/drycc/controller-sdk-go/apps"
"github.com/drycc/controller-sdk-go/appsettings"
"github.com/drycc/controller-sdk-go/certs"
"github.com/drycc/controller-sdk-go/config"
"github.com/drycc/controller-sdk-go/domains"
"github.com/drycc/controller-sdk-go/gateways"
"github.com/drycc/controller-sdk-go/keys"
"github.com/drycc/controller-sdk-go/limits"
"github.com/drycc/controller-sdk-go/ps"
"github.com/drycc/controller-sdk-go/pts"
"github.com/drycc/controller-sdk-go/releases"
"github.com/drycc/controller-sdk-go/resources"
"github.com/drycc/controller-sdk-go/routes"
"github.com/drycc/controller-sdk-go/services"
"github.com/drycc/controller-sdk-go/tokens"
"github.com/drycc/controller-sdk-go/volumes"
"github.com/drycc/controller-sdk-go/workspaces"
"github.com/drycc/controller-sdk-go/workspaces/members"
"github.com/drycc/workflow-cli/internal/loader"
"github.com/drycc/workflow-cli/pkg/settings"
"github.com/spf13/cobra"
)
// Completion is an interface for command completion functions
type Completion interface {
CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
}
// AppCompletion provides completion for application names
type AppCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of application names for completion
func (c *AppCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if apps, _, err := apps.List(s.Client, -1); err == nil {
var results []string
for _, app := range apps {
if strings.HasPrefix(app.ID, toComplete) {
results = append(results, app.ID)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// CertCompletion provides completion for certificate names
type CertCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of certificate names for completion
func (c *CertCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if certs, _, err := certs.List(s.Client, appID, -1); err == nil {
var results []string
for _, cert := range certs {
if strings.HasPrefix(cert.Name, toComplete) {
results = append(results, cert.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// CertDomainTachCompletion provides completion for certificate domain names
type CertDomainTachCompletion struct {
AppID *string
ConfigFile *string
}
// CompletionFunc returns a list of certificate domain names for completion
func (c *CertDomainTachCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
certCompletion := CertCompletion{AppID: c.AppID, ConfigFile: c.ConfigFile}
return certCompletion.CompletionFunc(cmd, args, toComplete)
}
domainCompletion := DomainCompletion{AppID: c.AppID, ArgsLen: 1, ConfigFile: c.ConfigFile}
return domainCompletion.CompletionFunc(cmd, args, toComplete)
}
// ConfigPtsGroupArgsCompletion provides completion for config pts group arguments
type ConfigPtsGroupArgsCompletion struct {
AppID *string
ConfigFile *string
}
// CompletionFunc returns a list of config pts group arguments for completion
func (c *ConfigPtsGroupArgsCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
ptsCompletion := PtsCompletion{AppID: c.AppID, ConfigFile: c.ConfigFile}
return ptsCompletion.CompletionFunc(cmd, args, toComplete)
}
groups := args[1:]
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil {
if config, err := config.List(s.Client, appID, -1); err == nil {
var results []string
for _, value := range config.Values {
if value.Group != "" && strings.HasPrefix(value.Group, toComplete) {
if !slices.Contains(groups, value.Group) && !slices.Contains(results, value.Group) {
results = append(results, value.Group)
}
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ConfigGroupCompletion provides completion for config group names
type ConfigGroupCompletion struct {
AppID *string
ConfigFile *string
ArgsLen int
}
// CompletionFunc returns a list of config group names for completion
func (c *ConfigGroupCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if config, err := config.List(s.Client, appID, -1); err == nil {
var results []string
for _, value := range config.Values {
if value.Group != "" && strings.HasPrefix(value.Group, toComplete) {
results = append(results, value.Group)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// DomainCompletion provides completion for domain names
type DomainCompletion struct {
AppID *string
ConfigFile *string
ArgsLen int
}
// CompletionFunc returns a list of domain names for completion
func (c *DomainCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if domains, _, err := domains.List(s.Client, appID, -1); err == nil {
var results []string
for _, domain := range domains {
if strings.HasPrefix(domain.Domain, toComplete) {
results = append(results, domain.Domain)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// GatewayProtocolCompletion provides completion for gateway protocols
type GatewayProtocolCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of gateway protocols for completion
func (c *GatewayProtocolCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
protocols := []string{"TCP", "UDP", "TLS", "HTTP", "HTTPS"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, protocol := range protocols {
if strings.HasPrefix(protocol, toComplete) {
results = append(results, protocol)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// GatewayNameCompletion provides completion for gateway names
type GatewayNameCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of gateway names for completion
func (c *GatewayNameCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if gateways, _, err := gateways.List(s.Client, appID, -1); err == nil {
for _, gateway := range gateways {
if strings.HasPrefix(gateway.Name, toComplete) {
results = append(results, gateway.Name)
}
}
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// LifecycleHandlerCompletion provides completion for lifecycle handler types
type LifecycleHandlerCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of lifecycle handler types for completion
func (c *LifecycleHandlerCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
healthTypes := []string{"postStart", "preStop"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, healthType := range healthTypes {
if strings.HasPrefix(healthType, toComplete) {
results = append(results, healthType)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// LifecycleActionTypeCompletion provides completion for lifecycle action types
type LifecycleActionTypeCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of lifecycle action types for completion
func (c *LifecycleActionTypeCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
lifecycleActionTypes := []string{"exec", "sleep", "httpGet", "tcpSocket"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, lifecycleActionType := range lifecycleActionTypes {
if strings.HasPrefix(lifecycleActionType, toComplete) {
results = append(results, lifecycleActionType)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// LifecycleCompletion provides completion for health checks
type LifecycleCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of health checks for completion
func (c *LifecycleCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
lifecycleHandlerCompletion := LifecycleHandlerCompletion{ArgsLen: 0, ConfigFile: c.ConfigFile}
return lifecycleHandlerCompletion.CompletionFunc(cmd, args, toComplete)
}
lifecycleActionTypeCompletion := LifecycleActionTypeCompletion{ArgsLen: 1, ConfigFile: c.ConfigFile}
return lifecycleActionTypeCompletion.CompletionFunc(cmd, args, toComplete)
}
// HealthcheckProbeTypeCompletion provides completion for probe types
type HealthcheckProbeTypeCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of health check types for completion
func (p *HealthcheckProbeTypeCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
probeTypes := []string{"startupProbe", "livenessProbe", "readinessProbe"}
if p.ArgsLen < 0 || len(args) == p.ArgsLen {
var results []string
for _, probeType := range probeTypes {
if strings.HasPrefix(probeType, toComplete) {
results = append(results, probeType)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// HealthcheckProbeActionTypeCompletion provides completion for health check probe action types
type HealthcheckProbeActionTypeCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of health check probe action types for completion
func (c *HealthcheckProbeActionTypeCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
probeTypes := []string{"httpGet", "exec", "tcpSocket"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, probeType := range probeTypes {
if strings.HasPrefix(probeType, toComplete) {
results = append(results, probeType)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// HealthCheckCompletion provides completion for health checks
type HealthCheckCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of health checks for completion
func (c *HealthCheckCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
healthcheckProbeTypeCompletion := HealthcheckProbeTypeCompletion{ArgsLen: 0, ConfigFile: c.ConfigFile}
return healthcheckProbeTypeCompletion.CompletionFunc(cmd, args, toComplete)
}
healthcheckProbeActionTypeCompletion := HealthcheckProbeActionTypeCompletion{ArgsLen: 1, ConfigFile: c.ConfigFile}
return healthcheckProbeActionTypeCompletion.CompletionFunc(cmd, args, toComplete)
}
// ServiceProtocolCompletion provides completion for service protocols
type ServiceProtocolCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of service protocols for completion
func (c *ServiceProtocolCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
protocols := []string{"TCP", "UDP", "TLS", "SCTP"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, protocol := range protocols {
if strings.HasPrefix(protocol, toComplete) {
results = append(results, protocol)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// KeyCompletion provides completion for SSH key names
type KeyCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of SSH key names for completion
func (c *KeyCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if keys, _, err := keys.List(s.Client, -1); err == nil {
var results []string
for _, key := range keys {
if strings.HasPrefix(key.ID, toComplete) {
results = append(results, key.ID)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// TokenCompletion provides completion for tokens
type TokenCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of tokens for completion
func (c *TokenCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if tokens, _, err := tokens.List(s.Client, -1); err == nil {
var results []string
for _, token := range tokens {
if strings.HasPrefix(token.UUID, toComplete) {
results = append(results, token.UUID)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// LabelCompletion provides completion for label names
type LabelCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of label names for completion
func (c *LabelCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if appsettings, err := appsettings.List(s.Client, appID); err == nil {
var results []string
for key := range appsettings.Label {
if strings.HasPrefix(key, toComplete) {
if !slices.Contains(args, key) {
results = append(results, key)
}
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// LimitSpecCompletion provides completion for limit specifications
type LimitSpecCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of limit specifications for completion
func (c *LimitSpecCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if specs, _, err := limits.Specs(s.Client, toComplete, -1); err == nil {
var results []string
for _, sepc := range specs {
results = append(results, sepc.ID)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// LimitSetPlanCompletion provides completion for limit set plans
type LimitSetPlanCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of limit set plans for completion
func (c *LimitSetPlanCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if strings.Contains(toComplete, "=") {
var results []string
parts := strings.Split(toComplete, "=")
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if plans, _, err := limits.Plans(s.Client, "", 0, 0, -1); err == nil {
for _, plan := range plans {
if strings.HasPrefix(plan.ID, parts[1]) {
results = append(results, fmt.Sprintf("%s=%s", parts[0], plan.ID))
}
}
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
ptsSetArgsCompletion := PtsSetArgsCompletion{
PtsCompletion: &PtsCompletion{AppID: c.AppID, ArgsLen: -1, ConfigFile: c.ConfigFile},
}
return ptsSetArgsCompletion.CompletionFunc(cmd, args, toComplete)
}
// WorkspaceCompletion provides completion for workspace names
type WorkspaceCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of workspace names for completion
func (c *WorkspaceCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if wkspaces, _, err := workspaces.List(s.Client, -1); err == nil {
var results []string
for _, ws := range wkspaces {
if strings.HasPrefix(ws.Name, toComplete) {
results = append(results, ws.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// WorkspaceMemberCompletion provides completion for workspace member usernames
type WorkspaceMemberCompletion struct {
Workspace *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of workspace member usernames for completion
func (c *WorkspaceMemberCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
workspace := *c.Workspace
if workspace == "" && len(args) > 0 {
workspace = args[0]
}
if workspace != "" {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if mems, _, err := members.List(s.Client, workspace, -1); err == nil {
var results []string
for _, m := range mems {
if strings.HasPrefix(m.User, toComplete) {
results = append(results, m.User)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// WorkspaceRoleCompletion provides completion for workspace member roles
type WorkspaceRoleCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of workspace roles for completion
func (c *WorkspaceRoleCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
roles := []string{"admin", "member", "viewer"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, role := range roles {
if strings.HasPrefix(role, toComplete) {
results = append(results, role)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// WorkspaceUpdateCompletion provides completion for workspace update command
type WorkspaceUpdateCompletion struct {
ConfigFile *string
}
// CompletionFunc returns completion for workspace update positional args
func (c *WorkspaceUpdateCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// First arg: workspace name
workspaceCompletion := WorkspaceCompletion{ArgsLen: 0, ConfigFile: c.ConfigFile}
return workspaceCompletion.CompletionFunc(cmd, args, toComplete)
}
// Second arg: member username
workspace := args[0]
memberCompletion := WorkspaceMemberCompletion{Workspace: &workspace, ArgsLen: 1, ConfigFile: c.ConfigFile}
return memberCompletion.CompletionFunc(cmd, args, toComplete)
}
// WorkspaceRemoveCompletion provides completion for workspace remove command
type WorkspaceRemoveCompletion struct {
ConfigFile *string
}
// CompletionFunc returns completion for workspace remove positional args
func (c *WorkspaceRemoveCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// First arg: workspace name
workspaceCompletion := WorkspaceCompletion{ArgsLen: 0, ConfigFile: c.ConfigFile}
return workspaceCompletion.CompletionFunc(cmd, args, toComplete)
}
// Second arg: member username
workspace := args[0]
memberCompletion := WorkspaceMemberCompletion{Workspace: &workspace, ArgsLen: 1, ConfigFile: c.ConfigFile}
return memberCompletion.CompletionFunc(cmd, args, toComplete)
}
// PsCompletion provides completion for process names
type PsCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of process names for completion
func (c *PsCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if pods, _, err := ps.List(s.Client, appID, -1); err == nil {
var results []string
for _, pod := range pods {
if strings.HasPrefix(pod.Name, toComplete) {
results = append(results, pod.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// PtsCompletion provides completion for pts names
type PtsCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of pts names for completion
func (c *PtsCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if ptypes, _, err := pts.List(s.Client, appID, -1); err == nil {
for _, ptype := range ptypes {
if strings.HasPrefix(ptype.Name, toComplete) {
if !slices.Contains(args, ptype.Name) {
results = append(results, ptype.Name)
}
}
}
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// PtsArgsCompletion provides completion for process type arguments.
type PtsArgsCompletion struct {
*PtsCompletion
}
// CompletionFunc returns a list of pts arguments for completion
func (c *PtsArgsCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
completes, _ := c.PtsCompletion.CompletionFunc(cmd, args, toComplete)
for _, complete := range completes {
if !slices.Contains(args, complete) {
results = append(results, complete)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
// PtsSetArgsCompletion provides completion for process type scale arguments.
type PtsSetArgsCompletion struct {
*PtsCompletion
}
// CompletionFunc returns a list of process type scale arguments for completion.
func (c *PtsSetArgsCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
completion := PtsCompletion{AppID: c.AppID, ArgsLen: -1, ConfigFile: c.ConfigFile}
completes, _ := completion.CompletionFunc(cmd, args, toComplete)
for _, complete := range completes {
hasArgs := false
result := fmt.Sprintf("%s=", complete)
for _, arg := range args {
if strings.HasPrefix(arg, result) {
hasArgs = true
break
}
}
if !hasArgs {
results = append(results, result)
}
}
return results, cobra.ShellCompDirectiveNoSpace
}
// ResourceServiceCompletion provides completion for resource services
type ResourceServiceCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of resource services for completion
func (c *ResourceServiceCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if services, _, err := resources.Services(s.Client, -1); err == nil {
var results []string
for _, service := range services {
if strings.HasPrefix(service.Name, toComplete) {
results = append(results, service.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ResourcePlanCompletion provides completion for resource plans
type ResourcePlanCompletion struct {
Service string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of resource plans for completion
func (c *ResourcePlanCompletion) CompletionFunc(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if s, err := settings.Load(*c.ConfigFile); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
var results []string
if services, _, err := resources.Plans(s.Client, c.Service, -1); err == nil {
for _, service := range services {
results = append(results, service.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ResourceCreateCompletion provides completion for resource creation
type ResourceCreateCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of resource creation options for completion
func (c *ResourceCreateCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
resourceServiceCompletion := ResourceServiceCompletion{ArgsLen: 0, ConfigFile: c.ConfigFile}
return resourceServiceCompletion.CompletionFunc(cmd, args, toComplete)
}
resourcePlanCompletion := ResourcePlanCompletion{Service: args[0], ArgsLen: 1, ConfigFile: c.ConfigFile}
return resourcePlanCompletion.CompletionFunc(cmd, args, toComplete)
}
// ResourceCompletion provides completion for resources
type ResourceCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of resources for completion
func (c *ResourceCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if resources, _, err := resources.List(s.Client, appID, -1); err == nil {
var results []string
for _, resource := range resources {
if strings.HasPrefix(resource.Name, toComplete) {
results = append(results, resource.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ResourceUpdateCompletion provides completion for resource update arguments.
type ResourceUpdateCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of resource update arguments for completion.
func (c *ResourceUpdateCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
ResourceCompletion := ResourceCompletion{AppID: c.AppID, ArgsLen: -1, ConfigFile: c.ConfigFile}
return ResourceCompletion.CompletionFunc(cmd, args, toComplete)
} else if len(args) == 1 {
resourceServiceCompletion := ResourceServiceCompletion{ArgsLen: -1, ConfigFile: c.ConfigFile}
return resourceServiceCompletion.CompletionFunc(cmd, args, toComplete)
} else if len(args) == 2 {
resourcePlanCompletion := ResourcePlanCompletion{Service: args[1], ArgsLen: -1, ConfigFile: c.ConfigFile}
return resourcePlanCompletion.CompletionFunc(cmd, args, toComplete)
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ReleaseCompletion provides completion for release versions.
type ReleaseCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of release versions for completion.
func (c *ReleaseCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if releases, _, err := releases.List(s.Client, appID, "", -1); err == nil {
var results []string
toComplete = strings.TrimPrefix(toComplete, "v")
for _, release := range releases {
version := strconv.Itoa(release.Version)
if strings.HasPrefix(version, toComplete) {
results = append(results, version)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// RouteKindCompletion provides completion for route kinds.
type RouteKindCompletion struct {
AppID *string
ArgsLen int
}
// CompletionFunc returns a list of route kinds for completion.
func (c *RouteKindCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
kinds := []string{"HTTPRoute", "TCPRoute", "UDPRoute", "GRPCRoute", "TLSRoute"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, kind := range kinds {
if strings.HasPrefix(kind, toComplete) {
results = append(results, kind)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// RouteCompletion provides completion for routes
type RouteCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of routes for completion
func (c *RouteCompletion) CompletionFunc(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if routes, _, err := routes.List(s.Client, appID, -1); err == nil {
var results []string
for _, route := range routes {
results = append(results, route.Name)
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// ServiceCompletion provides completion for services
type ServiceCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of services for completion
func (c *ServiceCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if services, err := services.List(s.Client, appID); err == nil {
var results []string
for _, service := range services {
if strings.HasPrefix(service.Ptype, toComplete) {
results = append(results, service.Ptype)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// TagCompletion provides completion for tags
type TagCompletion struct {
AppID *string
Ptype *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of tags for completion
func (c *TagCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if config, err := config.List(s.Client, appID, -1); err == nil {
var results []string
for tag := range config.Tags[*c.Ptype] {
if strings.HasPrefix(tag, toComplete) {
if !slices.Contains(args, tag) {
results = append(results, tag)
}
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// TLSActionCompletion provides completion for TLS actions
type TLSActionCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of TLS actions for completion
func (c *TLSActionCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
actions := []string{"redirect", "passthrough"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, action := range actions {
if strings.HasPrefix(action, toComplete) {
results = append(results, action)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// VolumeCompletion provides completion for volumes
type VolumeCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of volumes for completion
func (c *VolumeCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if appID, s, err := loader.LoadAppSettings(*c.ConfigFile, *c.AppID); err == nil && (c.ArgsLen < 0 || len(args) == c.ArgsLen) {
if volumes, _, err := volumes.List(s.Client, appID, -1); err == nil {
var results []string
for _, volume := range volumes {
if strings.HasPrefix(volume.Name, toComplete) {
results = append(results, volume.Name)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// VolumeTypeCompletion provides completion for volume types
type VolumeTypeCompletion struct {
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of volume types for completion
func (c *VolumeTypeCompletion) CompletionFunc(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
types := []string{"static", "dynamic"}
if c.ArgsLen < 0 || len(args) == c.ArgsLen {
var results []string
for _, t := range types {
if strings.HasPrefix(t, toComplete) {
results = append(results, t)
}
}
return results, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
// VolumesMountCompletion provides completion for volume mounts
type VolumesMountCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of volume mount options for completion
func (c *VolumesMountCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
volumeCompletion := VolumeCompletion{AppID: c.AppID, ArgsLen: 0, ConfigFile: c.ConfigFile}
return volumeCompletion.CompletionFunc(cmd, args, toComplete)
}
ptsArgCompletion := PtsArgsCompletion{
PtsCompletion: &PtsCompletion{AppID: c.AppID, ArgsLen: -1, ConfigFile: c.ConfigFile},
}
return ptsArgCompletion.CompletionFunc(cmd, args, toComplete)
}
// VolumesUnmountCompletion provides completion for volume unmounts
type VolumesUnmountCompletion struct {
AppID *string
ArgsLen int
ConfigFile *string
}
// CompletionFunc returns a list of volume unmount options for completion
func (c *VolumesUnmountCompletion) CompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
volumeCompletion := VolumeCompletion{AppID: c.AppID, ArgsLen: 0, ConfigFile: c.ConfigFile}
return volumeCompletion.CompletionFunc(cmd, args, toComplete)
}