Repository navigation
Expand file tree
/
Copy pathexecutor.go
More file actions
407 lines (375 loc) · 12.5 KB
/
Copy pathexecutor.go
File metadata and controls
407 lines (375 loc) · 12.5 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)
type Executor struct {
shell string
flag string
}
func NewExecutor() *Executor {
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/C"
} else {
shell = os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}
flag = "-lc"
}
return &Executor{shell: shell, flag: flag}
}
// stripShebang removes any shebang line (#!...) from the beginning of script content.
// Used for backward compatibility with old DB records that stored scripts with #!/bin/bash.
func stripShebang(content string) string {
s := strings.TrimSpace(content)
if strings.HasPrefix(s, "#!") {
if idx := strings.Index(s, "\n"); idx != -1 {
return s[idx+1:]
}
return ""
}
return s
}
// OpenInTerminal opens a terminal and runs the resolved script.
// Each LaunchFn receives the raw script body and handles its own quoting.
func (e *Executor) OpenInTerminal(terminalID string, scriptContent string, workingDir string) error {
body := stripShebang(scriptContent)
body = strings.TrimSpace(body)
defs := e.terminalDefs()
if terminalID != "" {
for _, d := range defs {
if d.ID == terminalID && e.terminalExists(d) && d.LaunchFn != nil {
return d.LaunchFn(e, body, workingDir)
}
}
}
for _, d := range defs {
if e.terminalExists(d) && d.LaunchFn != nil {
return d.LaunchFn(e, body, workingDir)
}
}
return fmt.Errorf("no terminal emulator found")
}
func shellQuoteDir(dir string) string {
if !strings.Contains(dir, `'`) {
return `'` + dir + `'`
}
escaped := strings.ReplaceAll(dir, `'`, `'"'"'`)
return `'` + escaped + `'`
}
// terminalDef defines how to detect and launch a terminal emulator
type terminalDef struct {
ID string
Name string
Paths []string // candidate binary paths or app bundle paths
IsApp bool // macOS .app bundle (use osascript to launch)
LaunchFn func(e *Executor, cmdText string, workingDir string) error
}
// GetAvailableTerminals returns all terminal emulators detected on the current system.
func (e *Executor) GetAvailableTerminals() []TerminalInfo {
defs := e.terminalDefs()
var result []TerminalInfo
for _, d := range defs {
if e.terminalExists(d) {
result = append(result, TerminalInfo{ID: d.ID, Name: d.Name})
}
}
if result == nil {
result = []TerminalInfo{}
}
return result
}
func resolveDarwinBin(plain, bundleBin string) string {
if _, err := exec.LookPath(plain); err != nil {
return bundleBin
}
return plain
}
func (e *Executor) terminalExists(d terminalDef) bool {
for _, p := range d.Paths {
if d.IsApp {
if _, err := os.Stat(p); err == nil {
return true
}
} else {
if _, err := exec.LookPath(p); err == nil {
return true
}
}
}
return false
}
func (e *Executor) terminalDefs() []terminalDef {
switch runtime.GOOS {
case "darwin":
return e.darwinTerminals()
case "linux":
return e.linuxTerminals()
case "windows":
return e.windowsTerminals()
}
return nil
}
func (e *Executor) darwinTerminals() []terminalDef {
osa := func(appName, script string) func(*Executor, string, string) error {
return func(_ *Executor, body string, workingDir string) error {
body = strings.TrimRight(body, "\n")
if workingDir != "" {
body = fmt.Sprintf("cd %s && %s", shellQuoteDir(workingDir), body)
}
asEscaped := strings.ReplaceAll(body, `\`, `\\`)
asEscaped = strings.ReplaceAll(asEscaped, `"`, `\"`)
s := fmt.Sprintf(script, asEscaped)
return exec.Command("osascript", "-e", s).Start()
}
}
alacrittyBin, alacrittyBundle := "alacritty", "/Applications/Alacritty.app/Contents/MacOS/alacritty"
kittyBin, kittyBundle := "kitty", "/Applications/kitty.app/Contents/MacOS/kitty"
ghosttyBin, ghosttyBundle := "ghostty", "/Applications/Ghostty.app/Contents/MacOS/ghostty"
return []terminalDef{
{
ID: "terminal", Name: "Terminal", Paths: []string{"/System/Applications/Utilities/Terminal.app"}, IsApp: true,
LaunchFn: osa("Terminal", `tell application "Terminal"
do script "%s"
activate
end tell`),
},
{
ID: "iterm2", Name: "iTerm2", Paths: []string{"/Applications/iTerm.app"}, IsApp: true,
LaunchFn: osa("iTerm2", `tell application "iTerm2"
create window with default profile
tell current session of current window
write text "%s"
end tell
activate
end tell`),
},
{
ID: "warp", Name: "Warp", Paths: []string{"/Applications/Warp.app"}, IsApp: true,
LaunchFn: func(_ *Executor, body string, workingDir string) error {
body = strings.TrimRight(body, "\n")
if workingDir != "" {
body = fmt.Sprintf("cd %s && %s", shellQuoteDir(workingDir), body)
}
asEscaped := strings.ReplaceAll(body, `\`, `\\`)
asEscaped = strings.ReplaceAll(asEscaped, `"`, `\"`)
s := fmt.Sprintf(`tell application "Warp" to activate
delay 0.5
tell application "System Events" to keystroke "%s"
tell application "System Events" to key code 36`, asEscaped)
return exec.Command("osascript", "-e", s).Start()
},
},
{
ID: "alacritty", Name: "Alacritty", Paths: []string{alacrittyBin, alacrittyBundle}, IsApp: false,
LaunchFn: func(ex *Executor, body string, workingDir string) error {
args := []string{"-e", ex.shell, ex.flag, body + "; exec " + ex.shell}
if workingDir != "" {
args = append([]string{"--working-directory", workingDir}, args...)
}
bin := resolveDarwinBin(alacrittyBin, alacrittyBundle)
return exec.Command(bin, args...).Start()
},
},
{
ID: "kitty", Name: "Kitty", Paths: []string{kittyBin, kittyBundle}, IsApp: false,
LaunchFn: func(ex *Executor, body string, workingDir string) error {
args := []string{ex.shell, ex.flag, body + "; exec " + ex.shell}
if workingDir != "" {
args = append([]string{"--directory", workingDir}, args...)
}
bin := resolveDarwinBin(kittyBin, kittyBundle)
return exec.Command(bin, args...).Start()
},
},
{
ID: "ghostty", Name: "Ghostty", Paths: []string{ghosttyBin, ghosttyBundle}, IsApp: false,
LaunchFn: func(ex *Executor, body string, workingDir string) error {
args := []string{"-e", ex.shell, ex.flag, body + "; exec " + ex.shell}
if workingDir != "" {
args = append([]string{"--working-directory=" + workingDir}, args...)
}
bin := resolveDarwinBin(ghosttyBin, ghosttyBundle)
return exec.Command(bin, args...).Start()
},
},
{
ID: "hyper", Name: "Hyper", Paths: []string{"/Applications/Hyper.app"}, IsApp: true,
LaunchFn: func(_ *Executor, body string, workingDir string) error {
return exec.Command("open", "-a", "Hyper").Start()
},
},
}
}
func (e *Executor) linuxTerminals() []terminalDef {
shellExec := func(bin string, buildArgs func(shell, body string) []string, dirFlag func(dir string) []string) func(*Executor, string, string) error {
return func(ex *Executor, body string, workingDir string) error {
args := buildArgs(ex.shell, body)
if workingDir != "" && dirFlag != nil {
args = append(dirFlag(workingDir), args...)
}
return exec.Command(bin, args...).Start()
}
}
return []terminalDef{
{ID: "gnome-terminal", Name: "GNOME Terminal", Paths: []string{"gnome-terminal"},
LaunchFn: shellExec("gnome-terminal", func(sh, body string) []string {
return []string{"--", sh, "-c", body + "; exec " + sh}
}, func(dir string) []string {
return []string{"--working-directory=" + dir}
})},
{ID: "gnome-console", Name: "GNOME Console", Paths: []string{"kgx"},
LaunchFn: shellExec("kgx", func(sh, body string) []string {
escaped := strings.ReplaceAll(body, "'", `'\''`)
return []string{"-e", sh + " -c '" + escaped + "; exec " + sh + "'"}
}, func(dir string) []string {
return []string{"--working-directory=" + dir}
})},
{ID: "konsole", Name: "Konsole", Paths: []string{"konsole"},
LaunchFn: shellExec("konsole", func(sh, body string) []string {
return []string{"-e", sh, "-c", body + "; exec " + sh}
}, func(dir string) []string {
return []string{"--workdir", dir}
})},
{ID: "xfce4-terminal", Name: "XFCE Terminal", Paths: []string{"xfce4-terminal"},
LaunchFn: shellExec("xfce4-terminal", func(sh, body string) []string {
escaped := strings.ReplaceAll(body, "'", `'\''`)
return []string{"-e", sh + " -c '" + escaped + "; exec " + sh + "'"}
}, func(dir string) []string {
return []string{"--working-directory=" + dir}
})},
{ID: "alacritty", Name: "Alacritty", Paths: []string{"alacritty"},
LaunchFn: shellExec("alacritty", func(sh, body string) []string {
return []string{"-e", sh, "-c", body + "; exec " + sh}
}, func(dir string) []string {
return []string{"--working-directory", dir}
})},
{ID: "kitty", Name: "Kitty", Paths: []string{"kitty"},
LaunchFn: shellExec("kitty", func(sh, body string) []string {
return []string{sh, "-c", body + "; exec " + sh}
}, func(dir string) []string {
return []string{"--directory", dir}
})},
{ID: "ghostty", Name: "Ghostty", Paths: []string{"ghostty"},
LaunchFn: shellExec("ghostty", func(sh, body string) []string {
return []string{"-e", sh, "-c", body + "; exec " + sh}
}, func(dir string) []string {
return []string{"--working-directory=" + dir}
})},
{ID: "xterm", Name: "XTerm", Paths: []string{"xterm"},
LaunchFn: func(ex *Executor, body string, workingDir string) error {
body = strings.TrimRight(body, "\n")
if workingDir != "" {
body = fmt.Sprintf("cd %s && %s", shellQuoteDir(workingDir), body)
}
return exec.Command("xterm", "-e", ex.shell, "-c", body+"; exec "+ex.shell).Start()
}},
}
}
func (e *Executor) windowsTerminals() []terminalDef {
escapeCmdExe := func(body string) string {
s := strings.ReplaceAll(body, `"`, `""`)
s = strings.ReplaceAll(s, `%`, `%%`)
return `"` + s + `"`
}
return []terminalDef{
{ID: "windows-terminal", Name: "Windows Terminal", Paths: []string{"wt"},
LaunchFn: func(_ *Executor, body string, workingDir string) error {
args := []string{"cmd", "/k", escapeCmdExe(body)}
if workingDir != "" {
args = append([]string{"-d", workingDir}, args...)
}
return exec.Command("wt", args...).Start()
}},
{ID: "cmd", Name: "Command Prompt", Paths: []string{"cmd"},
LaunchFn: func(_ *Executor, body string, workingDir string) error {
cmdBody := escapeCmdExe(body)
if workingDir != "" {
cmdBody = fmt.Sprintf("cd /d %s && %s", escapeCmdExe(workingDir), cmdBody)
}
return exec.Command("cmd", "/c", "start", "cmd", "/k", cmdBody).Start()
}},
{ID: "pwsh", Name: "PowerShell", Paths: []string{"pwsh", "powershell"},
LaunchFn: func(_ *Executor, body string, workingDir string) error {
bin := "powershell"
if _, err := exec.LookPath("pwsh"); err == nil {
bin = "pwsh"
}
if workingDir != "" {
body = fmt.Sprintf("Set-Location -LiteralPath '%s' -ErrorAction Stop; %s", strings.ReplaceAll(workingDir, "'", "''"), body)
}
return exec.Command(bin, "-NoExit", "-Command", body).Start()
}},
}
}
// EvalDefaults evaluates CEL expressions in variable definitions and returns resolved defaults.
func (e *Executor) EvalDefaults(defs []VariableDefinition) map[string]string {
results := make(map[string]string, len(defs))
if len(defs) == 0 {
return results
}
env, err := cel.NewEnv(
cel.Function("now",
cel.Overload("now_void", nil, cel.StringType,
cel.FunctionBinding(func(args ...ref.Val) ref.Val {
return types.String(time.Now().Format(time.RFC3339))
}),
),
),
cel.Function("env",
cel.Overload("env_string", []*cel.Type{cel.StringType}, cel.StringType,
cel.UnaryBinding(func(val ref.Val) ref.Val {
key := string(val.(types.String))
return types.String(os.Getenv(key))
}),
),
),
cel.Function("date",
cel.Overload("date_string", []*cel.Type{cel.StringType}, cel.StringType,
cel.UnaryBinding(func(val ref.Val) ref.Val {
layout := string(val.(types.String))
return types.String(time.Now().Format(layout))
}),
),
),
)
if err != nil {
for _, d := range defs {
results[d.Name] = d.Default
}
return results
}
for _, d := range defs {
if d.Default == "" {
results[d.Name] = ""
continue
}
ast, issues := env.Compile(d.Default)
if issues != nil && issues.Err() != nil {
results[d.Name] = d.Default
continue
}
prg, err := env.Program(ast)
if err != nil {
results[d.Name] = d.Default
continue
}
out, _, err := prg.Eval(cel.NoVars())
if err != nil {
results[d.Name] = d.Default
continue
}
results[d.Name] = fmt.Sprintf("%v", out.Value())
}
return results
}