@@ -19,6 +19,7 @@ package cli
1919import (
2020 "context"
2121 "strings"
22+ "time"
2223
2324 "github.com/spf13/cobra"
2425 kbclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -30,128 +31,105 @@ import (
3031// completionFunc is the function signature for cobra's ValidArgsFunction.
3132type completionFunc = func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective )
3233
33- // CompleteBackupNames returns a completion function that lists backup names from the cluster.
34- func CompleteBackupNames (f client.Factory ) completionFunc {
34+ // completeNames is a generic helper that builds a completion function for any
35+ // Velero list type. The caller provides a constructor for the list object and a
36+ // callback that extracts resource names from it.
37+ func completeNames [L kbclient.ObjectList ](f client.Factory , newList func () L , getNames func (L ) []string ) completionFunc {
3538 return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
3639 kbClient , err := f .KubebuilderClient ()
3740 if err != nil {
3841 return nil , cobra .ShellCompDirectiveNoFileComp
3942 }
40- backups := new (velerov1api.BackupList )
41- if err := kbClient .List (context .TODO (), backups , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
43+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
44+ defer cancel ()
45+ list := newList ()
46+ if err := kbClient .List (ctx , list , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
4247 return nil , cobra .ShellCompDirectiveNoFileComp
4348 }
44- var names []string
45- for _ , b := range backups . Items {
46- if strings .HasPrefix (b . Name , toComplete ) {
47- names = append (names , b . Name )
49+ var filtered []string
50+ for _ , name := range getNames ( list ) {
51+ if strings .HasPrefix (name , toComplete ) {
52+ filtered = append (filtered , name )
4853 }
4954 }
50- return names , cobra .ShellCompDirectiveNoFileComp
55+ return filtered , cobra .ShellCompDirectiveNoFileComp
5156 }
5257}
5358
54- // CompleteRestoreNames returns a completion function that lists restore names from the cluster.
59+ func CompleteBackupNames (f client.Factory ) completionFunc {
60+ return completeNames (f ,
61+ func () * velerov1api.BackupList { return new (velerov1api.BackupList ) },
62+ func (l * velerov1api.BackupList ) []string {
63+ names := make ([]string , len (l .Items ))
64+ for i , b := range l .Items {
65+ names [i ] = b .Name
66+ }
67+ return names
68+ },
69+ )
70+ }
71+
5572func CompleteRestoreNames (f client.Factory ) completionFunc {
56- return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
57- kbClient , err := f .KubebuilderClient ()
58- if err != nil {
59- return nil , cobra .ShellCompDirectiveNoFileComp
60- }
61- restores := new (velerov1api.RestoreList )
62- if err := kbClient .List (context .TODO (), restores , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
63- return nil , cobra .ShellCompDirectiveNoFileComp
64- }
65- var names []string
66- for _ , r := range restores .Items {
67- if strings .HasPrefix (r .Name , toComplete ) {
68- names = append (names , r .Name )
73+ return completeNames (f ,
74+ func () * velerov1api.RestoreList { return new (velerov1api.RestoreList ) },
75+ func (l * velerov1api.RestoreList ) []string {
76+ names := make ([]string , len (l .Items ))
77+ for i , r := range l .Items {
78+ names [i ] = r .Name
6979 }
70- }
71- return names , cobra . ShellCompDirectiveNoFileComp
72- }
80+ return names
81+ },
82+ )
7383}
7484
75- // CompleteScheduleNames returns a completion function that lists schedule names from the cluster.
7685func CompleteScheduleNames (f client.Factory ) completionFunc {
77- return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
78- kbClient , err := f .KubebuilderClient ()
79- if err != nil {
80- return nil , cobra .ShellCompDirectiveNoFileComp
81- }
82- schedules := new (velerov1api.ScheduleList )
83- if err := kbClient .List (context .TODO (), schedules , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
84- return nil , cobra .ShellCompDirectiveNoFileComp
85- }
86- var names []string
87- for _ , s := range schedules .Items {
88- if strings .HasPrefix (s .Name , toComplete ) {
89- names = append (names , s .Name )
86+ return completeNames (f ,
87+ func () * velerov1api.ScheduleList { return new (velerov1api.ScheduleList ) },
88+ func (l * velerov1api.ScheduleList ) []string {
89+ names := make ([]string , len (l .Items ))
90+ for i , s := range l .Items {
91+ names [i ] = s .Name
9092 }
91- }
92- return names , cobra . ShellCompDirectiveNoFileComp
93- }
93+ return names
94+ },
95+ )
9496}
9597
96- // CompleteBackupStorageLocationNames returns a completion function that lists backup storage location names from the cluster.
9798func CompleteBackupStorageLocationNames (f client.Factory ) completionFunc {
98- return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
99- kbClient , err := f .KubebuilderClient ()
100- if err != nil {
101- return nil , cobra .ShellCompDirectiveNoFileComp
102- }
103- locations := new (velerov1api.BackupStorageLocationList )
104- if err := kbClient .List (context .TODO (), locations , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
105- return nil , cobra .ShellCompDirectiveNoFileComp
106- }
107- var names []string
108- for _ , l := range locations .Items {
109- if strings .HasPrefix (l .Name , toComplete ) {
110- names = append (names , l .Name )
99+ return completeNames (f ,
100+ func () * velerov1api.BackupStorageLocationList { return new (velerov1api.BackupStorageLocationList ) },
101+ func (l * velerov1api.BackupStorageLocationList ) []string {
102+ names := make ([]string , len (l .Items ))
103+ for i , loc := range l .Items {
104+ names [i ] = loc .Name
111105 }
112- }
113- return names , cobra . ShellCompDirectiveNoFileComp
114- }
106+ return names
107+ },
108+ )
115109}
116110
117- // CompleteVolumeSnapshotLocationNames returns a completion function that lists volume snapshot location names from the cluster.
118111func CompleteVolumeSnapshotLocationNames (f client.Factory ) completionFunc {
119- return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
120- kbClient , err := f .KubebuilderClient ()
121- if err != nil {
122- return nil , cobra .ShellCompDirectiveNoFileComp
123- }
124- locations := new (velerov1api.VolumeSnapshotLocationList )
125- if err := kbClient .List (context .TODO (), locations , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
126- return nil , cobra .ShellCompDirectiveNoFileComp
127- }
128- var names []string
129- for _ , l := range locations .Items {
130- if strings .HasPrefix (l .Name , toComplete ) {
131- names = append (names , l .Name )
112+ return completeNames (f ,
113+ func () * velerov1api.VolumeSnapshotLocationList { return new (velerov1api.VolumeSnapshotLocationList ) },
114+ func (l * velerov1api.VolumeSnapshotLocationList ) []string {
115+ names := make ([]string , len (l .Items ))
116+ for i , loc := range l .Items {
117+ names [i ] = loc .Name
132118 }
133- }
134- return names , cobra . ShellCompDirectiveNoFileComp
135- }
119+ return names
120+ },
121+ )
136122}
137123
138- // CompleteBackupRepositoryNames returns a completion function that lists backup repository names from the cluster.
139124func CompleteBackupRepositoryNames (f client.Factory ) completionFunc {
140- return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
141- kbClient , err := f .KubebuilderClient ()
142- if err != nil {
143- return nil , cobra .ShellCompDirectiveNoFileComp
144- }
145- repos := new (velerov1api.BackupRepositoryList )
146- if err := kbClient .List (context .TODO (), repos , & kbclient.ListOptions {Namespace : f .Namespace ()}); err != nil {
147- return nil , cobra .ShellCompDirectiveNoFileComp
148- }
149- var names []string
150- for _ , r := range repos .Items {
151- if strings .HasPrefix (r .Name , toComplete ) {
152- names = append (names , r .Name )
125+ return completeNames (f ,
126+ func () * velerov1api.BackupRepositoryList { return new (velerov1api.BackupRepositoryList ) },
127+ func (l * velerov1api.BackupRepositoryList ) []string {
128+ names := make ([]string , len (l .Items ))
129+ for i , r := range l .Items {
130+ names [i ] = r .Name
153131 }
154- }
155- return names , cobra . ShellCompDirectiveNoFileComp
156- }
132+ return names
133+ },
134+ )
157135}
0 commit comments