This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
/
completion.go
441 lines (361 loc) · 11.3 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
package cli
import (
"context"
"regexp"
"strings"
"github.com/acorn-io/baaah/pkg/typed"
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/channels"
"github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/project"
"github.com/spf13/cobra"
"k8s.io/utils/strings/slices"
)
var volumeClassVolumeFlagRegex = regexp.MustCompile("^.*class=([^,]*)$")
type completionFunc func(context.Context, client.Client, string) ([]string, error)
// These define instances when the completion should not happen and the default completion (usually file/directory
// completion should occur). For example, exec should only have one argument, everything after that is completed as
// default by the user's terminal.
type noCompletionOption func([]string) bool
type completion struct {
client ClientFactory
completionFunc completionFunc
successDirective cobra.ShellCompDirective
noCompletionOptions []noCompletionOption
}
func removeExistingArgs(result, args []string) []string {
for i := 0; i < len(result); {
if slices.Contains(args, result[i]) {
result = append(result[:i], result[i+1:]...)
} else {
i++
}
}
return result
}
func onlyNumArgs(n int) noCompletionOption {
return func(args []string) bool {
return len(args) >= n
}
}
func newCompletion(c ClientFactory, cf completionFunc) *completion {
return &completion{
client: c,
completionFunc: cf,
successDirective: cobra.ShellCompDirectiveNoFileComp,
}
}
func (a *completion) withShouldCompleteOptions(opts ...noCompletionOption) *completion {
a.noCompletionOptions = append(a.noCompletionOptions, opts...)
return a
}
func (a *completion) withSuccessDirective(d cobra.ShellCompDirective) *completion {
a.successDirective = d
return a
}
func (a *completion) complete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
for _, o := range a.noCompletionOptions {
if o(args) {
return nil, cobra.ShellCompDirectiveDefault
}
}
c, err := a.client.CreateDefault()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
result, err := a.completionFunc(cmd.Context(), c, toComplete)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return removeExistingArgs(result, args), a.successDirective
}
func appsThenSecretsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
result, err := appsCompletion(ctx, c, toComplete)
if err != nil || len(result) > 0 {
return result, err
}
return secretsCompletion(ctx, c, toComplete)
}
func appsThenContainersCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
// If the toComplete has a '.', then the user is looking for a container.
if strings.Contains(toComplete, ".") {
return containersCompletion(ctx, c, toComplete)
}
return appsCompletion(ctx, c, toComplete)
}
func appsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
var result []string
apps, err := c.AppList(ctx)
if err != nil {
return nil, err
}
for _, app := range apps {
if strings.HasPrefix(app.Name, toComplete) {
result = append(result, app.Name)
}
}
return result, nil
}
func containersCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
var result []string
containers, err := c.ContainerReplicaList(ctx, nil)
if err != nil {
return nil, err
}
for _, container := range containers {
if strings.HasPrefix(container.Name, toComplete) {
result = append(result, container.Name)
}
}
return result, nil
}
func jobsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
var result []string
jobs, err := c.JobList(ctx, nil)
if err != nil {
return nil, err
}
for _, job := range jobs {
if strings.HasPrefix(job.Name, toComplete) {
result = append(result, job.Name)
}
}
return result, nil
}
// acornContainerCompletion will complete the `-c` flag for various commands like exec. It must look at all apps and
// then for all containers on status.appSpec.Containers to produce a list of possibilities.
func acornContainerCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
apps, err := c.AppList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, app := range apps {
for _, entry := range typed.Sorted(app.Status.AppSpec.Containers) {
if strings.HasPrefix(entry.Key, toComplete) {
result = append(result, entry.Key)
}
}
}
return result, nil
}
// onlyAppsWithAcornContainer will look for completions for apps and pods containers when the container name is
// empty. If the container name is not empty, then it will only look for apps that have the specified container name in
// their appSpec.
func onlyAppsWithAcornContainer(containerName string) completionFunc {
return func(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
// If no container is specified, then look for apps and pod containers.
if containerName == "" {
return appsThenContainersCompletion(ctx, c, toComplete)
}
// If a container has been specified, then only produce completions of apps that have such a container.
apps, err := c.AppList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, app := range apps {
for container := range app.Status.AppSpec.Containers {
if container == containerName && strings.HasPrefix(app.Name, toComplete) {
result = append(result, app.Name)
break
}
}
}
return result, nil
}
}
func imagesCompletion(allowDigest bool) completionFunc {
return func(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
images, err := c.ImageList(ctx)
if err != nil {
return nil, err
}
var result []string
var tagMatched bool
for _, image := range images {
tagMatched = false
for _, tag := range image.Tags {
if strings.HasPrefix(tag, toComplete) {
result = append(result, tag)
tagMatched = true
}
}
// Don't include the digest if a tag matched.
if allowDigest && !tagMatched {
digest := strings.TrimPrefix(image.Digest, "sha256:")[:12]
if strings.HasPrefix(digest, toComplete) {
result = append(result, digest)
}
}
}
return result, nil
}
}
func credentialsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
credentials, err := c.CredentialList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, credential := range credentials {
if strings.HasPrefix(credential.Name, toComplete) {
result = append(result, credential.Name)
}
}
return result, nil
}
func volumesCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
volumes, err := c.VolumeList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, volume := range volumes {
if strings.HasPrefix(volume.Name, toComplete) {
result = append(result, volume.Name)
}
}
return result, nil
}
func secretsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
secrets, err := c.SecretList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, secret := range secrets {
if strings.HasPrefix(secret.Name, toComplete) {
result = append(result, secret.Name)
}
}
return result, nil
}
func projectsCompletion(f ClientFactory) completionFunc {
return func(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
var acornConfigFile string
if f != nil {
acornConfigFile = f.AcornConfigFile()
}
projects, _, err := project.List(ctx, false, project.Options{
AcornConfigFile: acornConfigFile,
})
if err != nil {
return nil, err
}
var result []string
for _, project := range projects {
if strings.HasPrefix(project, toComplete) {
result = append(result, project)
}
}
return result, nil
}
}
func volumeClassCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
volumeClasses, err := c.VolumeClassList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, volumeClass := range volumeClasses {
if strings.HasPrefix(volumeClass.Name, toComplete) {
result = append(result, volumeClass.Name)
}
}
return result, nil
}
func computeClassCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
computeClasses, err := c.ComputeClassList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, volumeClass := range computeClasses {
if strings.HasPrefix(volumeClass.Name, toComplete) {
result = append(result, volumeClass.Name)
}
}
return result, nil
}
func volumeFlagClassCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
matches := volumeClassVolumeFlagRegex.FindAllStringSubmatch(toComplete, 1)
if len(matches) == 0 {
return nil, nil
}
// If the regexp matches, then this is a flag completion of the form `-v foo:size=5G,class=..` and toComplete should be what follows `class=`
actualToComplete := matches[0][1]
result, err := volumeClassCompletion(ctx, c, actualToComplete)
// Trim the actualToComplete from the end so that we can append below to get the full completion.
toComplete = strings.TrimSuffix(toComplete, actualToComplete)
// Add the rest of the toComplete flag to the completion.
for i := range result {
result[i] = toComplete + result[i]
}
return result, err
}
func computeClassFlagCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
var (
computeClassFlagCompletion = regexp.MustCompile("^.*[,|=]([^,]*)$")
actualToComplete = toComplete
)
if matches := computeClassFlagCompletion.FindAllStringSubmatch(toComplete, 1); matches != nil {
actualToComplete = matches[0][1]
}
result, err := computeClassCompletion(ctx, c, actualToComplete)
// Trim the actualToComplete from the end so that we can append below to get the full completion.
toComplete = strings.TrimSuffix(toComplete, actualToComplete)
// Add the rest of the toComplete flag to the completion.
for i := range result {
result[i] = toComplete + result[i]
}
return result, err
}
func regionsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
regions, err := c.RegionList(ctx)
if err != nil {
return nil, err
}
var result []string
for _, region := range regions {
if strings.HasPrefix(region.Name, toComplete) {
result = append(result, region.Name)
}
}
return result, nil
}
func eventsCompletion(ctx context.Context, c client.Client, toComplete string) ([]string, error) {
var result []string
events, err := c.EventStream(ctx, &client.EventStreamOptions{})
if err != nil {
return nil, err
}
matched := make(map[string]struct{})
if err := channels.ForEach(ctx, events, func(e apiv1.Event) error {
completions := []string{
// Name prefix completion
e.Name,
}
if e.Resource != nil {
// Resource prefix completions
completions = append(completions,
e.Resource.String(),
e.Resource.Kind,
)
}
for _, completion := range completions {
if _, ok := matched[completion]; ok {
// Completion already added to results
return nil
}
if strings.HasPrefix(completion, toComplete) {
result = append(result, completion)
matched[completion] = struct{}{}
}
}
return nil
}); !channels.NilOrCanceled(err) {
return nil, err
}
return result, nil
}