Skip to content

Commit 176ac70

Browse files
Add vim support to brev open command
- Add EditorVim constant to editor types - Update validation logic in handleSetDefault and determineEditorType functions - Add vim case to openEditorByType and getEditorName functions - Implement openVim function following tmux pattern for SSH-based editor launching - Update help text and examples to include vim support - Command usage: brev open <instance> vim opens vim in home directory Co-Authored-By: Alec Fong <alecsanf@usc.edu>
1 parent 5b555fb commit 176ac70

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

pkg/cmd/open/open.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ const (
3434
EditorCursor = "cursor"
3535
EditorWindsurf = "windsurf"
3636
EditorTmux = "tmux"
37+
EditorVim = "vim"
3738
)
3839

3940
var (
40-
openLong = "[command in beta] This will open VS Code, Cursor, Windsurf, or tmux SSH-ed in to your instance. You must have the editor installed in your path."
41-
openExample = "brev open instance_id_or_name\nbrev open instance\nbrev open instance code\nbrev open instance cursor\nbrev open instance windsurf\nbrev open instance tmux\nbrev open --set-default cursor\nbrev open --set-default windsurf\nbrev open --set-default tmux"
41+
openLong = "[command in beta] This will open VS Code, Cursor, Windsurf, tmux, or vim SSH-ed in to your instance. You must have the editor installed in your path."
42+
openExample = "brev open instance_id_or_name\nbrev open instance\nbrev open instance code\nbrev open instance cursor\nbrev open instance windsurf\nbrev open instance tmux\nbrev open instance vim\nbrev open --set-default cursor\nbrev open --set-default windsurf\nbrev open --set-default tmux\nbrev open --set-default vim"
4243
)
4344

4445
type OpenStore interface {
@@ -100,14 +101,14 @@ func NewCmdOpen(t *terminal.Terminal, store OpenStore, noLoginStartStore OpenSto
100101
cmd.Flags().BoolVarP(&host, "host", "", false, "ssh into the host machine instead of the container")
101102
cmd.Flags().BoolVarP(&waitForSetupToFinish, "wait", "w", false, "wait for setup to finish")
102103
cmd.Flags().StringVarP(&directory, "dir", "d", "", "directory to open")
103-
cmd.Flags().StringVar(&setDefault, "set-default", "", "set default editor (code, cursor, windsurf, or tmux)")
104+
cmd.Flags().StringVar(&setDefault, "set-default", "", "set default editor (code, cursor, windsurf, tmux, or vim)")
104105

105106
return cmd
106107
}
107108

108109
func handleSetDefault(t *terminal.Terminal, editorType string) error {
109-
if editorType != EditorVSCode && editorType != EditorCursor && editorType != EditorWindsurf && editorType != EditorTmux {
110-
return fmt.Errorf("invalid editor type: %s. Must be 'code', 'cursor', 'windsurf', or 'tmux'", editorType)
110+
if editorType != EditorVSCode && editorType != EditorCursor && editorType != EditorWindsurf && editorType != EditorTmux && editorType != EditorVim {
111+
return fmt.Errorf("invalid editor type: %s. Must be 'code', 'cursor', 'windsurf', 'tmux', or 'vim'", editorType)
111112
}
112113

113114
homeDir, err := os.UserHomeDir()
@@ -131,8 +132,8 @@ func handleSetDefault(t *terminal.Terminal, editorType string) error {
131132
func determineEditorType(args []string) (string, error) {
132133
if len(args) == 2 {
133134
editorType := args[1]
134-
if editorType != EditorVSCode && editorType != EditorCursor && editorType != EditorWindsurf && editorType != EditorTmux {
135-
return "", fmt.Errorf("invalid editor type: %s. Must be 'code', 'cursor', 'windsurf', or 'tmux'", editorType)
135+
if editorType != EditorVSCode && editorType != EditorCursor && editorType != EditorWindsurf && editorType != EditorTmux && editorType != EditorVim {
136+
return "", fmt.Errorf("invalid editor type: %s. Must be 'code', 'cursor', 'windsurf', 'tmux', or 'vim'", editorType)
136137
}
137138
return editorType, nil
138139
}
@@ -353,12 +354,16 @@ func tryToInstallWindsurfExtensions(
353354
// Opens code editor. Attempts to install code in path if not installed already
354355
func getEditorName(editorType string) string {
355356
switch editorType {
357+
case EditorVSCode:
358+
return "VSCode"
356359
case EditorCursor:
357360
return "Cursor"
358361
case EditorWindsurf:
359362
return "Windsurf"
360363
case EditorTmux:
361364
return "tmux"
365+
case EditorVim:
366+
return "vim"
362367
default:
363368
return "VSCode"
364369
}
@@ -389,6 +394,8 @@ func openEditorByType(t *terminal.Terminal, editorType string, sshAlias string,
389394
return openWindsurf(sshAlias, path, tstore)
390395
case EditorTmux:
391396
return openTmux(sshAlias, path, tstore)
397+
case EditorVim:
398+
return openVim(sshAlias, path, tstore)
392399
default:
393400
tryToInstallExtensions(t, extensions)
394401
return openVsCode(sshAlias, path, tstore)
@@ -568,6 +575,23 @@ func openTmux(sshAlias string, path string, store OpenStore) error {
568575
return nil
569576
}
570577

578+
func openVim(sshAlias string, path string, store OpenStore) error {
579+
_ = store
580+
581+
vimCmd := fmt.Sprintf("ssh -t %s 'cd %s && vim'", sshAlias, path)
582+
583+
sshCmd := exec.Command("bash", "-c", vimCmd) // #nosec G204
584+
sshCmd.Stderr = os.Stderr
585+
sshCmd.Stdout = os.Stdout
586+
sshCmd.Stdin = os.Stdin
587+
588+
err := sshCmd.Run()
589+
if err != nil {
590+
return breverrors.WrapAndTrace(err)
591+
}
592+
return nil
593+
}
594+
571595
func ensureTmuxInstalled(sshAlias string) error {
572596
checkCmd := fmt.Sprintf("ssh %s 'which tmux >/dev/null 2>&1'", sshAlias)
573597
checkExec := exec.Command("bash", "-c", checkCmd) // #nosec G204

0 commit comments

Comments
 (0)