-
Notifications
You must be signed in to change notification settings - Fork 386
/
complete.go
613 lines (543 loc) · 14.5 KB
/
complete.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
package autocomplete
import (
"context"
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"reflect"
"sort"
"strings"
"github.com/earthly/earthly/buildcontext"
"github.com/earthly/earthly/domain"
"github.com/earthly/earthly/earthfile2llb"
"github.com/earthly/earthly/util/fileutil"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/urfave/cli/v2"
)
var (
errCompPointOutOfBounds = fmt.Errorf("COMP_POINT out of bounds")
)
func isLocalPath(path string) bool {
for _, prefix := range []string{".", "..", "/", "~"} {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}
func isCWDRoot() bool {
path, err := os.Getwd()
if err != nil {
return false
}
return path == "/"
}
func containsDirectories(path string) bool {
files, err := os.ReadDir(path)
if err != nil {
return false
}
for _, f := range files {
if f.IsDir() {
return true
}
}
return false
}
func getPotentialPaths(ctx context.Context, resolver *buildcontext.Resolver, gwClient gwclient.Client, prefix string) ([]string, error) {
if prefix == "." {
potentials := []string{}
if containsDirectories(".") {
potentials = append(potentials, "./")
}
if !isCWDRoot() {
potentials = append(potentials, "../")
}
return potentials, nil
}
if prefix == ".." {
return []string{"../"}, nil
}
currentUser, err := user.Current()
if err != nil {
return nil, err
}
var user string
expandedHomeLen := 0
if strings.HasPrefix(prefix, "~") {
users, err := fileutil.GetUserHomeDirs()
if err != nil {
return nil, err
}
// handle username completion
if !strings.Contains(prefix, "/") {
potentials := []string{}
for user := range users {
if strings.HasPrefix(user, prefix[1:]) {
potentials = append(potentials, "~"+user+"/")
}
}
return potentials, nil
}
// otherwise expand ~ into complete path
parts := strings.SplitN(prefix[1:], "/", 2)
user = parts[0]
rest := parts[1]
var homeDir string
if len(user) == 0 {
homeDir = currentUser.HomeDir
} else {
homeDir = users[user]
}
expandedHomeLen = len(currentUser.HomeDir) + 1
prefix = homeDir + "/" + rest
}
replaceHomePrefix := func(s string) string {
if expandedHomeLen == 0 {
return s
}
return "~" + user + "/" + s[expandedHomeLen:]
}
// handle targets
if strings.Contains(prefix, "+") {
splits := strings.SplitN(prefix, "+", 2)
if len(splits) < 2 {
return []string{}, nil
}
dirPath := splits[0]
targetToParse := prefix
if strings.HasSuffix(targetToParse, "+") {
targetToParse += "base"
}
target, err := domain.ParseTarget(targetToParse)
if err != nil {
return nil, err
}
targets, err := earthfile2llb.GetTargets(ctx, resolver, gwClient, target)
if err != nil {
return nil, err
}
if len(targets) == 0 {
// only suggest when Earthfile has no other targets
targets = append(targets, "base")
}
potentials := []string{}
for _, target := range targets {
s := dirPath + "+" + target + " "
if strings.HasPrefix(s, prefix) {
potentials = append(potentials, replaceHomePrefix(s))
}
}
return potentials, nil
}
usePrefixAsDir, err := fileutil.DirExists(prefix)
if err != nil {
return nil, err
}
if usePrefixAsDir && !strings.HasSuffix(prefix, "/") {
p, f := path.Split(prefix)
if hasEarthfile(f) {
usePrefixAsDir = false
}
files, err := os.ReadDir(p)
if err != nil {
return nil, err
}
for _, file := range files {
fileName := file.Name()
if strings.HasPrefix(fileName, f) && fileName != f {
usePrefixAsDir = false
break
}
}
}
var f, dir string
if usePrefixAsDir {
dir = prefix
} else {
dir, f = path.Split(prefix)
}
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
paths := []string{}
for _, file := range files {
if strings.HasPrefix(file.Name(), f) {
if file.IsDir() {
s := path.Join(dir, file.Name())
if strings.HasPrefix(prefix, "./") {
s = "./" + s
}
if hasEarthfile(s) {
paths = append(paths, replaceHomePrefix(s)+"+")
}
if hasSubDirs(s) {
paths = append(paths, replaceHomePrefix(s)+"/")
}
}
}
}
if prefix != "./" && !strings.HasSuffix(prefix, "/./") { // dont suggest parent directory for "./"
if abs, _ := filepath.Abs(dir); abs != "/" { // if Abs fails, we will suggest "/../"
if strings.HasSuffix(prefix, "/.") {
paths = append(paths, prefix+"./")
}
if strings.HasSuffix(prefix, "/") {
paths = append(paths, prefix+"../")
}
}
}
return paths, nil
}
func getPotentialTargetBuildArgs(ctx context.Context, resolver *buildcontext.Resolver, gwClient gwclient.Client, targetStr string) ([]string, error) {
target, err := domain.ParseTarget(targetStr)
if err != nil {
return nil, err
}
envArgs, err := earthfile2llb.GetTargetArgs(ctx, resolver, gwClient, target)
if err != nil {
return nil, err
}
return envArgs, nil
}
func getPotentialArtifactBuildArgs(ctx context.Context, resolver *buildcontext.Resolver, gwClient gwclient.Client, artifactStr string) ([]string, error) {
artifact, err := domain.ParseArtifact(artifactStr)
if err != nil {
return nil, err
}
return getPotentialTargetBuildArgs(ctx, resolver, gwClient, artifact.Target.String())
}
// isVisibleFlag returns if a flag is hidden or not
// this code comes from https://github.com/urfave/cli/blob/d648edd48d89ef3a841b1ec75c2ebbd4de5f748f/flag.go#L136
func isVisibleFlag(fl cli.Flag) bool {
fv := reflect.ValueOf(fl)
for fv.Kind() == reflect.Ptr {
fv = reflect.Indirect(fv)
}
field := fv.FieldByName("Hidden")
if !field.IsValid() || !field.Bool() {
return true
}
return false
}
func getCmd(name string, cmds []*cli.Command) *cli.Command {
for _, c := range cmds {
if name == c.Name {
return c
}
}
return nil
}
func getVisibleFlags(flags []cli.Flag) []string {
visibleFlags := []string{}
for _, f := range flags {
if isVisibleFlag(f) {
for _, n := range f.Names() {
if len(n) > 1 {
visibleFlags = append(visibleFlags, n)
}
}
}
}
return visibleFlags
}
func isBooleanFlag(flags []cli.Flag, flagName string) (isBool bool, flagFound bool) {
if flagName == "" {
return false, false
}
isShort := true
if strings.HasPrefix(flagName, "--") {
flagName = flagName[2:]
isShort = false
} else {
flagName = flagName[1:]
}
_ = isShort // short flags are not suggested; perhaps one day?
for _, f := range flags {
for _, n := range f.Names() {
if n == flagName {
_, ok := f.(*cli.BoolFlag)
return ok, true
}
}
}
return false, false
}
func isFlagValidAndRequiresValue(flags []cli.Flag, flagName string) bool {
isBool, ok := isBooleanFlag(flags, flagName)
return ok && !isBool
}
// padStrings takes an array of strings and returns a new array where each
// string element has been padded with a prefix and suffix
func padStrings(flags []string, prefix, suffix string) []string {
padded := make([]string, len(flags))
for i, s := range flags {
padded[i] = prefix + s + suffix
}
return padded
}
func getVisibleCommands(commands []*cli.Command) []string {
visibleCommands := []string{}
for _, cmd := range commands {
if !cmd.Hidden {
visibleCommands = append(visibleCommands, cmd.Name)
}
}
return visibleCommands
}
type completeState int
const (
unknownState completeState = iota
rootState // 1
flagState // 2
flagValueState // 3
commandState // 4
targetState // 5
targetFlagState // 6
artifactFlagState // 7
endOfSuggestionsState // 8
)
type FlagValuePotentialFn func(ctx context.Context, prefix string) []string
// GetPotentials returns a list of potential arguments for shell auto completion
// NOTE: you can cause earthly to run this command with:
//
// COMP_LINE="earthly -" COMP_POINT=$(echo -n $COMP_LINE | wc -c) go run cmd/earthly/main.go
func GetPotentials(ctx context.Context, resolver *buildcontext.Resolver, gwClient gwclient.Client, compLine string, compPoint int, app *cli.App, cloudClient cloudListClient) ([]string, error) {
if compPoint > len(compLine) {
return nil, errCompPointOutOfBounds
}
compLine = compLine[:compPoint]
subCommands := app.Commands
// TODO all the urfave/cli commands need to be moved out of the main package
// so they could be directly referenced rather than storing a list of strings of seen commands
commandValues := []string{}
getPrevCommand := func() string {
n := len(commandValues) - 2
if n >= 0 {
return commandValues[n]
}
return ""
}
flagValues := map[string]string{}
flagValuePotentialFuncs := map[string]FlagValuePotentialFn{
"--org": func(ctx context.Context, prefix string) []string {
if cloudClient == nil {
return []string{}
}
orgs, err := cloudClient.ListOrgs(ctx)
if err != nil {
return []string{}
}
potentials := []string{}
for _, org := range orgs {
potentials = append(potentials, org.Name)
}
return potentials
},
"--project": func(ctx context.Context, prefix string) []string {
if cloudClient == nil {
return []string{}
}
org, ok := flagValues["--org"]
if !ok {
return []string{}
}
projects, err := cloudClient.ListProjects(ctx, org)
if err != nil {
return []string{}
}
potentials := []string{}
for _, project := range projects {
potentials = append(potentials, project.Name)
}
return potentials
},
"--satellite": func(ctx context.Context, prefix string) []string {
if cloudClient == nil {
return []string{}
}
org, ok := flagValues["--org"]
if !ok {
return []string{}
}
satellites, err := cloudClient.ListSatellites(ctx, org, false)
if err != nil {
return []string{}
}
potentials := []string{}
for _, sat := range satellites {
potentials = append(potentials, sat.Name)
}
return potentials
},
}
// getWord returns the next word and a boolean if it is valid
// TODO this function does not handle escaped space, e.g.
// earthly --build-arg key="value with space" +mytarget will fail
hasNextWord := len(compLine) > 0
getWord := func() (string, bool) {
if !hasNextWord {
return "", false
}
i := strings.Index(compLine, " ")
if i < 0 {
word := compLine
compLine = ""
hasNextWord = false
return word, true
}
word := compLine[:i]
compLine = compLine[(i + 1):]
return word, true
}
// remove first word which is most likely "earthly", or "/some/path/to/earthly", etc.
prevWord, _ := getWord()
state := rootState
var target string
var artifactMode bool
var flag string
var cmd *cli.Command
getFlags := func() []cli.Flag {
if cmd != nil {
return cmd.Flags
}
return app.Flags
}
for {
w, ok := getWord()
if !ok {
break
}
if state == flagValueState {
flagValues[flag] = prevWord
prevWord = ""
state = flagState
}
if state == flagState && isFlagValidAndRequiresValue(getFlags(), prevWord) {
state = flagValueState
flag = prevWord
} else if state == rootState || state == commandState || state == flagState {
if strings.HasPrefix(w, "-") {
if w == "-a" || w == "--artifact" {
artifactMode = true
}
state = flagState
} else {
// targets only work under the root command
if cmd == nil && (isLocalPath(w) || strings.HasPrefix(w, "+")) { // TODO switch to strings.Contains when remote resolving works
state = targetState
target = w
} else {
// must be under a command
foundCmd := getCmd(w, subCommands)
if foundCmd != nil {
subCommands = foundCmd.Subcommands
cmd = foundCmd
// TODO once urfave/cli commands are moved out of main, this should be removed (and instead the cmd pointer could simply be compared to determine which command we are referencing)
commandValues = append(commandValues, cmd.Name)
}
state = commandState
}
}
} else if state == targetState || state == targetFlagState {
if !strings.HasPrefix(w, "-") {
state = endOfSuggestionsState
} else {
if strings.HasSuffix(w, "=") {
state = endOfSuggestionsState
} else {
if artifactMode {
state = artifactFlagState
} else {
state = targetFlagState
}
}
}
}
prevWord = w
}
var potentials []string
switch state {
case flagState:
if cmd != nil {
potentials = getVisibleFlags(cmd.Flags)
} else {
potentials = getVisibleFlags(app.Flags)
// append flags that urfav/cli automatically include
potentials = append(potentials, "version", "help")
}
potentials = padStrings(potentials, "--", " ")
case flagValueState:
if fn, ok := flagValuePotentialFuncs[flag]; ok {
potentials = append(potentials, fn(ctx, prevWord)...)
}
case rootState, commandState:
if cmd != nil {
potentials = getVisibleCommands(cmd.Subcommands)
potentials = padStrings(potentials, "", " ")
// TODO this should be tied to the instance of the command (and not just command Name value); but that means moving lots out of the main package
if getPrevCommand() == "satellite" && (cmd.Name == "inspect" || cmd.Name == "rm" || cmd.Name == "select" || cmd.Name == "sleep" || cmd.Name == "update" || cmd.Name == "wake") {
potentials = append(potentials, flagValuePotentialFuncs["--satellite"](ctx, "")...)
}
} else {
potentials = getVisibleCommands(app.Commands)
potentials = padStrings(potentials, "", " ")
if containsDirectories(".") {
potentials = append(potentials, "./")
}
if fileutil.FileExistsBestEffort("Earthfile") {
potentials = append(potentials, "+")
}
}
case targetState:
var err error
potentials, err = getPotentialPaths(ctx, resolver, gwClient, prevWord)
if err != nil {
return nil, err
}
case targetFlagState:
var err error
potentials, err = getPotentialTargetBuildArgs(ctx, resolver, gwClient, target)
if err != nil {
return nil, err
}
potentials = padStrings(potentials, "--", "=")
case artifactFlagState:
var err error
potentials, err = getPotentialArtifactBuildArgs(ctx, resolver, gwClient, target)
if err != nil {
return nil, err
}
potentials = padStrings(potentials, "--", "=")
}
filteredPotentials := []string{}
for _, s := range potentials {
if strings.HasPrefix(s, prevWord) {
filteredPotentials = append(filteredPotentials, s)
}
}
sort.Strings(filteredPotentials)
return filteredPotentials, nil
}
func hasEarthfile(dirPath string) bool {
info, err := os.Stat(path.Join(dirPath, "Earthfile"))
if err != nil {
return false
}
return !info.IsDir()
}
func hasSubDirs(dirPath string) bool {
files, err := os.ReadDir(dirPath)
if err != nil {
return false
}
for _, file := range files {
if file.IsDir() {
return true
}
}
return false
}