Skip to content

Commit 43bd7be

Browse files
committed
refactor: improve open.go error handling and consolidate editor commands
open.go: - Add 'terminal' to valid editors in error message - Use multierror for better multi-instance error aggregation - Add scanner error check for stdin reading util.go: - Extract common runEditorCommand helper for WSL compatibility - Simplify runVsCodeCommand, runCursorCommand, runWindsurfCommand
1 parent 12d4c66 commit 43bd7be

2 files changed

Lines changed: 24 additions & 48 deletions

File tree

pkg/cmd/open/open.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func NewCmdOpen(t *terminal.Terminal, store OpenStore, noLoginStartStore OpenSto
140140

141141
// Validate editor flag if provided
142142
if editor != "" && !isEditorType(editor) {
143-
return breverrors.NewValidationError(fmt.Sprintf("invalid editor: %s. Must be 'code', 'cursor', 'windsurf', or 'tmux'", editor))
143+
return breverrors.NewValidationError(fmt.Sprintf("invalid editor: %s. Must be 'code', 'cursor', 'windsurf', 'terminal', or 'tmux'", editor))
144144
}
145145

146146
// Get instance names and editor type from args or stdin
@@ -155,7 +155,7 @@ func NewCmdOpen(t *terminal.Terminal, store OpenStore, noLoginStartStore OpenSto
155155
}
156156

157157
// Open each instance
158-
var lastErr error
158+
var errors error
159159
for _, instanceName := range instanceNames {
160160
if len(instanceNames) > 1 {
161161
fmt.Fprintf(os.Stderr, "Opening %s...\n", instanceName)
@@ -164,14 +164,14 @@ func NewCmdOpen(t *terminal.Terminal, store OpenStore, noLoginStartStore OpenSto
164164
if err != nil {
165165
if len(instanceNames) > 1 {
166166
fmt.Fprintf(os.Stderr, "Error opening %s: %v\n", instanceName, err)
167-
lastErr = err
167+
errors = multierror.Append(errors, err)
168168
continue
169169
}
170170
return breverrors.WrapAndTrace(err)
171171
}
172172
}
173-
if lastErr != nil {
174-
return breverrors.NewValidationError("one or more instances failed to open")
173+
if errors != nil {
174+
return breverrors.WrapAndTrace(errors)
175175
}
176176
return nil
177177
},
@@ -216,6 +216,9 @@ func getInstanceNamesAndEditor(args []string, editorFlag string) ([]string, stri
216216
names = append(names, name)
217217
}
218218
}
219+
if err := scanner.Err(); err != nil {
220+
return nil, "", breverrors.WrapAndTrace(err)
221+
}
219222
}
220223

221224
if len(names) == 0 {
@@ -710,10 +713,7 @@ end tell`, command)
710713
}
711714
}
712715

713-
func openTerminal(sshAlias string, path string, store OpenStore) error {
714-
_ = store // unused parameter required by interface
715-
_ = path // unused, just opens SSH
716-
716+
func openTerminal(sshAlias string, _ string, _ OpenStore) error {
717717
sshCmd := fmt.Sprintf("ssh %s", sshAlias)
718718
err := openInNewTerminalWindow(sshCmd)
719719
if err != nil {

pkg/util/util.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ func runWindowsExeInWSL(exePath string, args []string) ([]byte, error) {
5959

6060
cmd := exec.Command("cmd.exe", cmdArgs...) // #nosec G204
6161
output, err := cmd.CombinedOutput()
62-
return output, breverrors.WrapAndTrace(err)
62+
if err != nil {
63+
return nil, breverrors.WrapAndTrace(err)
64+
}
65+
return output, nil
6366
}
6467

6568
// This package should only be used as a holding pattern to be later moved into more specific packages
@@ -253,40 +256,27 @@ func runManyCursorCommand(cursorpaths []string, args []string) ([]byte, error) {
253256
return nil, breverrors.WrapAndTrace(errs.ErrorOrNil())
254257
}
255258

256-
func runVsCodeCommand(vscodepath string, args []string) ([]byte, error) {
259+
// runEditorCommand runs an editor executable with the given args, handling WSL compatibility
260+
func runEditorCommand(path string, args []string) ([]byte, error) {
257261
// In WSL, Windows .exe files need to be run through cmd.exe
258-
if isWSL() && (strings.HasSuffix(vscodepath, ".exe") || strings.HasPrefix(vscodepath, "/mnt/")) {
259-
res, err := runWindowsExeInWSL(vscodepath, args)
260-
if err != nil {
261-
return nil, breverrors.WrapAndTrace(err)
262-
}
263-
return res, nil
262+
if isWSL() && (strings.HasSuffix(path, ".exe") || strings.HasPrefix(path, "/mnt/")) {
263+
return runWindowsExeInWSL(path, args)
264264
}
265265

266-
cmd := exec.Command(vscodepath, args...) // #nosec G204
266+
cmd := exec.Command(path, args...) // #nosec G204
267267
res, err := cmd.CombinedOutput()
268268
if err != nil {
269269
return nil, breverrors.WrapAndTrace(err)
270270
}
271271
return res, nil
272272
}
273273

274-
func runCursorCommand(cursorpath string, args []string) ([]byte, error) {
275-
// In WSL, Windows .exe files need to be run through cmd.exe
276-
if isWSL() && (strings.HasSuffix(cursorpath, ".exe") || strings.HasPrefix(cursorpath, "/mnt/")) {
277-
res, err := runWindowsExeInWSL(cursorpath, args)
278-
if err != nil {
279-
return nil, breverrors.WrapAndTrace(err)
280-
}
281-
return res, nil
282-
}
274+
func runVsCodeCommand(vscodepath string, args []string) ([]byte, error) {
275+
return runEditorCommand(vscodepath, args)
276+
}
283277

284-
cmd := exec.Command(cursorpath, args...) // #nosec G204
285-
res, err := cmd.CombinedOutput()
286-
if err != nil {
287-
return nil, breverrors.WrapAndTrace(err)
288-
}
289-
return res, nil
278+
func runCursorCommand(cursorpath string, args []string) ([]byte, error) {
279+
return runEditorCommand(cursorpath, args)
290280
}
291281

292282
func runManyWindsurfCommand(windsurfpaths []string, args []string) ([]byte, error) {
@@ -303,21 +293,7 @@ func runManyWindsurfCommand(windsurfpaths []string, args []string) ([]byte, erro
303293
}
304294

305295
func runWindsurfCommand(windsurfpath string, args []string) ([]byte, error) {
306-
// In WSL, Windows .exe files need to be run through cmd.exe
307-
if isWSL() && (strings.HasSuffix(windsurfpath, ".exe") || strings.HasPrefix(windsurfpath, "/mnt/")) {
308-
res, err := runWindowsExeInWSL(windsurfpath, args)
309-
if err != nil {
310-
return nil, breverrors.WrapAndTrace(err)
311-
}
312-
return res, nil
313-
}
314-
315-
cmd := exec.Command(windsurfpath, args...) // #nosec G204
316-
res, err := cmd.CombinedOutput()
317-
if err != nil {
318-
return nil, breverrors.WrapAndTrace(err)
319-
}
320-
return res, nil
296+
return runEditorCommand(windsurfpath, args)
321297
}
322298

323299
var commonVSCodePaths = []string{

0 commit comments

Comments
 (0)