-
Notifications
You must be signed in to change notification settings - Fork 386
/
autocomplete.go
100 lines (85 loc) · 2.97 KB
/
autocomplete.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
package helper
import (
"context"
"fmt"
"math"
"os"
"path/filepath"
"runtime/debug"
"strconv"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/pkg/errors"
"github.com/earthly/earthly/cmd/earthly/base"
"github.com/earthly/earthly/autocomplete"
"github.com/earthly/earthly/buildcontext"
"github.com/earthly/earthly/cloud"
"github.com/earthly/earthly/conslogging"
"github.com/earthly/earthly/util/cliutil"
)
// to enable autocomplete, enter
// complete -o nospace -C "/path/to/earthly" earthly
func AutoComplete(ctx context.Context, cli *base.CLI) {
_, found := os.LookupEnv("COMP_LINE")
if !found {
return
}
cli.SetConsole(cli.Console().WithLogLevel(conslogging.Silent))
err := autoCompleteImp(ctx, cli)
if err != nil {
errToLog := err
logDir, err := cliutil.GetOrCreateEarthlyDir(cli.Flags().InstallationName)
if err != nil {
os.Exit(1)
}
logFile := filepath.Join(logDir, "autocomplete.log")
err = os.MkdirAll(logDir, 0755)
if err != nil {
os.Exit(1)
}
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
os.Exit(1)
}
fmt.Fprintf(f, "error during autocomplete: %s\n", errToLog)
os.Exit(1)
}
os.Exit(0)
}
func autoCompleteImp(ctx context.Context, cli *base.CLI) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.Errorf("recovered panic in autocomplete %s: %s", r, debug.Stack())
}
}()
compLine := os.Getenv("COMP_LINE") // full command line
compPoint := os.Getenv("COMP_POINT") // where the cursor is
compPointInt, err := strconv.ParseUint(compPoint, 10, 64)
if err != nil {
return err
}
if !(compPointInt > 0 && compPointInt < math.MaxInt) {
err = errors.Errorf("compPointInt is out of bounds.")
return err
}
gitLookup := buildcontext.NewGitLookup(cli.Console(), cli.Flags().SSHAuthSock)
resolver := buildcontext.NewResolver(nil, gitLookup, cli.Console(), "", cli.Flags().GitBranchOverride, "", 0, "")
var gwClient gwclient.Client // TODO this is a nil pointer which causes a panic if we try to expand a remotely referenced earthfile
// it's expensive to create this gwclient, so we need to implement a lazy eval which returns it when required.
potentials, err := autocomplete.GetPotentials(ctx, resolver, gwClient, compLine, int(compPointInt), cli.App(), autocomplete.NewCachedCloudClient(cli.Flags().InstallationName, getCloudClientForAutoCompleter(ctx, cli)))
if err != nil {
return err
}
for _, p := range potentials {
fmt.Printf("%s\n", p)
}
return nil
}
func getCloudClientForAutoCompleter(ctx context.Context, cli *base.CLI) *cloud.Client {
// TODO these need to be set outside of urfave/cli; since the auto-competer happens without envoking it
// maybe we can half-parse them? or just set the defaults?
cli.Flags().CloudHTTPAddr = "https://api.earthly.dev"
cli.Flags().CloudGRPCAddr = "ci.earthly.dev:443"
cli.Flags().SSHAuthSock = os.Getenv("SSH_AUTH_SOCK")
cloudClient, _ := NewCloudClient(cli) // best effort
return cloudClient
}