forked from redhat-developer/odo
/
completionhandlers.go
424 lines (365 loc) · 14 KB
/
completionhandlers.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
package completion
import (
"fmt"
appsv1 "github.com/openshift/api/apps/v1"
"github.com/openshift/odo/pkg/application"
"github.com/openshift/odo/pkg/catalog"
"github.com/openshift/odo/pkg/component"
componentlabels "github.com/openshift/odo/pkg/component/labels"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/service"
"github.com/openshift/odo/pkg/storage"
"github.com/openshift/odo/pkg/url"
"github.com/openshift/odo/pkg/util"
"github.com/posener/complete"
"github.com/spf13/cobra"
"strings"
)
// ServiceCompletionHandler provides service name completion for the current project and application
var ServiceCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
services, err := service.List(context.Client, context.Application)
if err != nil {
return completions
}
for _, class := range services.Items {
if args.commands[class.ObjectMeta.Name] {
return nil
}
completions = append(completions, class.ObjectMeta.Name)
}
return
}
// ServiceClassCompletionHandler provides catalog service class name completion
var ServiceClassCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
services, err := context.Client.GetClusterServiceClasses()
if err != nil {
complete.Log("error retrieving services")
return completions
}
complete.Log(fmt.Sprintf("found %d services", len(services)))
for _, class := range services {
if args.commands[class.Spec.ExternalName] {
return nil
}
completions = append(completions, class.Spec.ExternalName)
}
return
}
// ServicePlanCompletionHandler provides completion for the the plan of a selected service
var ServicePlanCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
// if we have less than two arguments, it means the user didn't specify the name of the service
// meaning that there is no point in providing suggestions
if len(args.original.Completed) < 2 {
complete.Log("Couldn't extract the service name")
return completions
}
inputServiceName := args.original.Completed[1]
complete.Log(fmt.Sprintf("Using input: serviceName = %s", inputServiceName))
clusterServiceClass, err := context.Client.GetClusterServiceClass(inputServiceName)
if err != nil {
complete.Log("Error retrieving details of service")
return completions
}
servicePlans, err := context.Client.GetClusterPlansFromServiceName(clusterServiceClass.Name)
if err != nil {
complete.Log("Error retrieving details of plans of service")
return completions
}
for _, servicePlan := range servicePlans {
completions = append(completions, servicePlan.Spec.ExternalName)
}
return completions
}
// ServiceParameterCompletionHandler provides completion for the parameter names of a selected service and plan
var ServiceParameterCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
if len(args.original.Completed) < 2 {
complete.Log("Couldn't extract the service name")
return completions
}
inputServiceName := args.original.Completed[1]
inputPlanName := args.flagValues["plan"]
complete.Log(fmt.Sprintf("Using input: serviceName = %s, servicePlan = %s", inputServiceName, inputPlanName))
_, servicePlans, err := service.GetServiceClassAndPlans(context.Client, inputServiceName)
if err != nil {
complete.Log("Error retrieving details of service")
return completions
}
var matchingServicePlan *service.ServicePlan = nil
if len(servicePlans) == 0 {
complete.Log("Service has no plans so no parameters can be found")
return completions
} else if len(servicePlans) == 1 && inputPlanName == "" {
matchingServicePlan = &servicePlans[0]
} else {
for _, sp := range servicePlans {
servicePlan := sp
if servicePlan.Name == inputPlanName {
matchingServicePlan = &servicePlan
break
}
}
if matchingServicePlan == nil {
complete.Log("No service plan for the service matched the supplied plan name")
return completions
}
}
alreadyAddedParameters := args.flagValues["parameters"]
for _, servicePlanParameter := range matchingServicePlan.Parameters {
// don't add the parameter if it's already on the command line
if !strings.Contains(alreadyAddedParameters, servicePlanParameter.Name) {
completions = append(completions, servicePlanParameter.Name)
}
}
return completions
}
// AppCompletionHandler provides completion for the app commands
var AppCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
applications, err := application.List(context.Client)
if err != nil {
return completions
}
for _, app := range applications {
if args.commands[app] {
return nil
}
completions = append(completions, app)
}
return
}
// FileCompletionHandler provides suggestions for files and directories
var FileCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = append(completions, complete.PredictFiles("*").Predict(args.original)...)
return
}
// ProjectNameCompletionHandler provides project name completion
var ProjectNameCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
projects, err := context.Client.GetProjectNames()
if err != nil {
return completions
}
for _, project := range projects {
// we found the project name in the list which means
// that the project name has been already selected by the user so no need to suggest more
if args.commands[project] {
return nil
}
completions = append(completions, project)
}
return completions
}
// URLCompletionHandler provides completion for the url commands
var URLCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
urls, err := url.ListPushed(context.Client, context.Component(), context.Application)
if err != nil {
return completions
}
for _, url := range urls.Items {
// we found the url in the list which means
// that the url name has been already selected by the user so no need to suggest more
if args.commands[url.Name] {
return nil
}
completions = append(completions, url.Name)
}
return
}
// StorageDeleteCompletionHandler provides storage name completion for storage delete
var StorageDeleteCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
localConfig, err := config.New()
if err != nil {
return completions
}
storageList, err := localConfig.StorageList()
if err != nil {
return completions
}
for _, storage := range storageList {
// we found the storage name in the list which means
// that the storage name has been already selected by the user so no need to suggest more
if args.commands[storage.Name] {
return nil
}
completions = append(completions, storage.Name)
}
return completions
}
// StorageMountCompletionHandler provides storage name completion for storage mount
var StorageMountCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
storages, err := storage.ListUnmounted(context.Client, context.Application)
if err != nil {
return completions
}
for _, storage := range storages.Items {
// we found the storage name in the list which means
// that the storage name has been already selected by the user so no need to suggest more
if args.commands[storage.Name] {
return nil
}
completions = append(completions, storage.Name)
}
return completions
}
// StorageUnMountCompletionHandler provides storage name completion for storage unmount
var StorageUnMountCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
storageList, err := storage.ListMounted(context.Client, context.Component(), context.Application)
if err != nil {
return completions
}
for _, storage := range storageList.Items {
// we found the storage name in the list which means
// that the storage name has been already selected by the user so no need to suggest more
if args.commands[storage.Name] {
return nil
}
completions = append(completions, storage.Name)
}
return completions
}
// CreateCompletionHandler provides component type completion in odo create command
var CreateCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
comps := &completions
found := false
tasks := util.NewConcurrentTasks(2)
tasks.Add(util.ConcurrentTask{ToRun: func(errChannel chan error) {
catalogList, _ := catalog.ListComponents(context.Client)
for _, builder := range catalogList.Items {
if args.commands[builder.Name] {
found = true
return
}
if len(builder.Spec.NonHiddenTags) > 0 {
*comps = append(*comps, builder.Name)
}
}
}})
tasks.Add(util.ConcurrentTask{ToRun: func(errChannel chan error) {
components, _ := catalog.ListDevfileComponents("")
for _, devfile := range components.Items {
if args.commands[devfile.Name] {
found = true
return
}
*comps = append(*comps, devfile.Name)
}
}})
_ = tasks.Run()
if found {
return nil
}
return completions
}
// LinkCompletionHandler provides completion for the odo link
// The function returns both components and services
var LinkCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
components, err := component.List(context.Client, context.Application, nil)
if err != nil {
return completions
}
services, err := service.List(context.Client, context.Application)
if err != nil {
return completions
}
for _, component := range components.Items {
// we found the name in the list which means
// that the name has been already selected by the user so no need to suggest more
if val, ok := args.commands[component.Name]; ok && val {
return nil
}
// we don't want to show the selected component as a target for linking, so we remove it from the suggestions
if component.Name != context.Component() {
completions = append(completions, component.Name)
}
}
for _, service := range services.Items {
// we found the name in the list which means
// that the name has been already selected by the user so no need to suggest more
if val, ok := args.commands[service.ObjectMeta.Name]; ok && val {
return nil
}
completions = append(completions, service.ObjectMeta.Name)
}
return completions
}
// LinkCompletionHandler provides completion for the odo link
// The function returns both components and services
var UnlinkCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
// first we need to retrieve the current component
dcOfCurrentComponent, err := getDCOfComponent(context)
if err != nil || dcOfCurrentComponent == nil {
return completions
}
components, err := component.List(context.Client, context.Application, nil)
if err != nil {
return completions
}
services, err := service.List(context.Client, context.Application)
if err != nil {
return completions
}
for _, component := range components.Items {
// we found the name in the list which means
// that the name has been already selected by the user so no need to suggest more
if val, ok := args.commands[component.Name]; ok && val {
return nil
}
// we don't want to show the selected component as a target for linking, so we remove it from the suggestions
if component.Name != context.Component() {
// we also need to make sure that this component has been linked to the current component
for _, envFromSourceName := range dcOfCurrentComponent.Spec.Template.Spec.Containers[0].EnvFrom {
if strings.Contains(envFromSourceName.SecretRef.Name, component.Name) {
completions = append(completions, component.Name)
}
}
}
}
for _, service := range services.Items {
// we found the name in the list which means
// that the name has been already selected by the user so no need to suggest more
if val, ok := args.commands[service.ObjectMeta.Name]; ok && val {
return nil
}
// we also need to make sure that this component has been linked to the current component
for _, envFromSourceName := range dcOfCurrentComponent.Spec.Template.Spec.Containers[0].EnvFrom {
if strings.Contains(envFromSourceName.SecretRef.Name, service.ObjectMeta.Name) {
completions = append(completions, service.ObjectMeta.Name)
}
}
}
return completions
}
func getDCOfComponent(context *genericclioptions.Context) (*appsv1.DeploymentConfig, error) {
componentLabels := componentlabels.GetLabels(context.Component(), context.Application, false)
componentSelector := util.ConvertLabelsToSelector(componentLabels)
return context.Client.GetOneDeploymentConfigFromSelector(componentSelector)
}
// ComponentNameCompletionHandler provides component name completion
var ComponentNameCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
components, err := component.List(context.Client, context.Application, nil)
if err != nil {
return completions
}
for _, component := range components.Items {
// we found the component name in the list which means
// that the component name has been already selected by the user so no need to suggest more
if args.commands[component.Name] {
return nil
}
completions = append(completions, component.Name)
}
return completions
}