|
| 1 | +package dap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/docker/buildx/build" |
| 11 | + "github.com/docker/cli/cli-plugins/metadata" |
| 12 | + "github.com/google/go-dap" |
| 13 | + "github.com/google/shlex" |
| 14 | + "github.com/pkg/errors" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +func (d *Adapter[C]) Evaluate(ctx Context, req *dap.EvaluateRequest, resp *dap.EvaluateResponse) error { |
| 19 | + if req.Arguments.Context != "repl" { |
| 20 | + return errors.Errorf("unsupported evaluate context: %s", req.Arguments.Context) |
| 21 | + } |
| 22 | + |
| 23 | + args, err := shlex.Split(req.Arguments.Expression) |
| 24 | + if err != nil { |
| 25 | + return errors.Wrapf(err, "cannot parse expression") |
| 26 | + } |
| 27 | + |
| 28 | + if len(args) == 0 { |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + var t *thread |
| 33 | + if req.Arguments.FrameId > 0 { |
| 34 | + if t = d.getThreadByFrameID(req.Arguments.FrameId); t == nil { |
| 35 | + return errors.Errorf("no thread with frame id %d", req.Arguments.FrameId) |
| 36 | + } |
| 37 | + } else { |
| 38 | + if t = d.getFirstThread(); t == nil { |
| 39 | + return errors.New("no paused thread") |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + cmd := d.replCommands(ctx, t, resp) |
| 44 | + cmd.SetArgs(args) |
| 45 | + cmd.SetErr(d.Out()) |
| 46 | + if err := cmd.Execute(); err != nil { |
| 47 | + fmt.Fprintf(d.Out(), "ERROR: %+v\n", err) |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (d *Adapter[C]) replCommands(ctx Context, t *thread, resp *dap.EvaluateResponse) *cobra.Command { |
| 53 | + rootCmd := &cobra.Command{} |
| 54 | + |
| 55 | + execCmd := &cobra.Command{ |
| 56 | + Use: "exec", |
| 57 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | + if !d.supportsExec { |
| 59 | + return errors.New("cannot exec without runInTerminal client capability") |
| 60 | + } |
| 61 | + return t.Exec(ctx, args, resp) |
| 62 | + }, |
| 63 | + } |
| 64 | + rootCmd.AddCommand(execCmd) |
| 65 | + return rootCmd |
| 66 | +} |
| 67 | + |
| 68 | +func (t *thread) Exec(ctx Context, args []string, eresp *dap.EvaluateResponse) (retErr error) { |
| 69 | + cfg := &build.InvokeConfig{Tty: true} |
| 70 | + if len(cfg.Entrypoint) == 0 && len(cfg.Cmd) == 0 { |
| 71 | + cfg.Entrypoint = []string{"/bin/sh"} // launch shell by default |
| 72 | + cfg.Cmd = []string{} |
| 73 | + cfg.NoCmd = false |
| 74 | + } |
| 75 | + |
| 76 | + ctr, err := build.NewContainer(ctx, t.rCtx, cfg) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + defer func() { |
| 81 | + if retErr != nil { |
| 82 | + ctr.Cancel() |
| 83 | + } |
| 84 | + }() |
| 85 | + |
| 86 | + dir, err := os.MkdirTemp("", "buildx-dap-exec") |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + defer func() { |
| 91 | + if retErr != nil { |
| 92 | + os.RemoveAll(dir) |
| 93 | + } |
| 94 | + }() |
| 95 | + |
| 96 | + socketPath := filepath.Join(dir, "s.sock") |
| 97 | + l, err := net.Listen("unix", socketPath) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + go func() { |
| 103 | + defer os.RemoveAll(dir) |
| 104 | + t.runExec(l, ctr, cfg) |
| 105 | + }() |
| 106 | + |
| 107 | + // TODO: this should work in standalone mode too. |
| 108 | + docker := os.Getenv(metadata.ReexecEnvvar) |
| 109 | + req := &dap.RunInTerminalRequest{ |
| 110 | + Request: dap.Request{ |
| 111 | + Command: "runInTerminal", |
| 112 | + }, |
| 113 | + Arguments: dap.RunInTerminalRequestArguments{ |
| 114 | + Kind: "integrated", |
| 115 | + Args: []string{docker, "buildx", "dap", "attach", socketPath}, |
| 116 | + Env: map[string]any{ |
| 117 | + "BUILDX_EXPERIMENTAL": "1", |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + resp := ctx.Request(req) |
| 123 | + if !resp.GetResponse().Success { |
| 124 | + return errors.New(resp.GetResponse().Message) |
| 125 | + } |
| 126 | + |
| 127 | + eresp.Body.Result = fmt.Sprintf("Started process attached to %s.", socketPath) |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func (t *thread) runExec(l net.Listener, ctr *build.Container, cfg *build.InvokeConfig) { |
| 132 | + defer l.Close() |
| 133 | + defer ctr.Cancel() |
| 134 | + |
| 135 | + conn, err := l.Accept() |
| 136 | + if err != nil { |
| 137 | + return |
| 138 | + } |
| 139 | + defer conn.Close() |
| 140 | + |
| 141 | + // start a background goroutine to politely refuse any subsequent connections. |
| 142 | + go func() { |
| 143 | + for { |
| 144 | + conn, err := l.Accept() |
| 145 | + if err != nil { |
| 146 | + return |
| 147 | + } |
| 148 | + fmt.Fprint(conn, "Error: Already connected to exec instance.") |
| 149 | + conn.Close() |
| 150 | + } |
| 151 | + }() |
| 152 | + ctr.Exec(context.Background(), cfg, conn, conn, conn) |
| 153 | +} |
0 commit comments