-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathopen.go
More file actions
199 lines (169 loc) · 5.54 KB
/
open.go
File metadata and controls
199 lines (169 loc) · 5.54 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
package vscode
import (
"context"
"errors"
"fmt"
"os/exec"
"runtime"
"strings"
"github.com/loft-sh/devpod/pkg/command"
"github.com/loft-sh/log"
"github.com/skratchdot/open-golang/open"
)
func Open(ctx context.Context, workspace, folder string, newWindow bool, flavor Flavor, log log.Logger) error {
log.Infof("Starting %s...", flavor.DisplayName())
cliErr := openViaCLI(ctx, workspace, folder, newWindow, flavor, log)
if cliErr == nil {
return nil
}
browserErr := openViaBrowser(workspace, folder, newWindow, flavor, log)
if browserErr == nil {
return nil
}
return errors.Join(cliErr, browserErr)
}
func openViaBrowser(workspace, folder string, newWindow bool, flavor Flavor, log log.Logger) error {
var protocol string
switch flavor {
case FlavorStable:
protocol = `vscode://`
case FlavorInsiders:
protocol = `vscode-insiders://`
case FlavorCursor:
protocol = `cursor://`
case FlavorPositron:
protocol = `positron://`
case FlavorCodium:
protocol = `codium://`
case FlavorWindsurf:
protocol = `windsurf://`
case FlavorAntigravity:
protocol = `antigravity://`
default:
return fmt.Errorf("unknown flavor %s", flavor)
}
openURL := protocol + `vscode-remote/ssh-remote+` + workspace + `.devpod/` + folder
if newWindow {
openURL += "?windowId=_blank"
}
err := open.Run(openURL)
if err != nil {
log.Debugf("Starting %s caused error: %v", flavor, err)
log.Errorf("Seems like you don't have %s installed on your computer locally", flavor.DisplayName())
return err
}
return nil
}
func openViaCLI(ctx context.Context, workspace, folder string, newWindow bool, flavor Flavor, log log.Logger) error {
// try to find code cli
codePath := findCLI(flavor)
if codePath == "" {
return fmt.Errorf("couldn't find the %s binary", flavor)
}
sshExtension := "ms-vscode-remote.remote-ssh"
if flavor == FlavorCodium {
sshExtension = "jeanp413.open-remote-ssh"
}
// make sure ms-vscode-remote.remote-ssh is installed
out, err := exec.Command(codePath, "--list-extensions").Output()
if err != nil {
return command.WrapCommandError(out, err)
}
splitted := strings.Split(string(out), "\n")
found := false
foundContainers := false
for _, str := range splitted {
if strings.TrimSpace(str) == sshExtension {
found = true
} else if strings.TrimSpace(str) == "ms-vscode-remote.remote-containers" {
foundContainers = true
}
}
// install remote-ssh extension
if !found {
args := []string{"--install-extension", sshExtension}
log.Debugf("Run vscode command %s %s", codePath, strings.Join(args, " "))
out, err := exec.CommandContext(ctx, codePath, args...).Output()
if err != nil {
return fmt.Errorf("install ssh extension: %w", command.WrapCommandError(out, err))
}
}
// open vscode via cli
args := make([]string, 0, 5)
if foundContainers {
args = append(args, "--disable-extension", "ms-vscode-remote.remote-containers")
}
if newWindow {
args = append(args, "--new-window")
} else {
args = append(args, "--reuse-window")
}
// Needs to be separated by `=` because of windows
folderUriArg := fmt.Sprintf("--folder-uri=vscode-remote://ssh-remote+%s.devpod/%s", workspace, folder)
args = append(args, folderUriArg)
log.Debugf("Run %s command %s %s", flavor.DisplayName(), codePath, strings.Join(args, " "))
out, err = exec.CommandContext(ctx, codePath, args...).CombinedOutput()
if err != nil {
return command.WrapCommandError(out, err)
}
return nil
}
func findCLI(flavor Flavor) string {
if flavor == FlavorStable {
if command.Exists("code") {
return "code"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code") {
return "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"
}
return ""
}
if flavor == FlavorInsiders {
if command.Exists("code-insiders") {
return "code-insiders"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code") {
return "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code"
}
return ""
}
if flavor == FlavorCursor {
if command.Exists("cursor") {
return "cursor"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Cursor.app/Contents/Resources/app/bin/cursor") {
return "/Applications/Cursor.app/Contents/Resources/app/bin/cursor"
}
return ""
}
if flavor == FlavorPositron {
if command.Exists("positron") {
return "positron"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Positron.app/Contents/Resources/app/bin/positron") {
return "/Applications/Positron.app/Contents/Resources/app/bin/positron"
}
return ""
}
if flavor == FlavorCodium {
if command.Exists("codium") {
return "codium"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Codium.app/Contents/Resources/app/bin/codium") {
return "/Applications/Codium.app/Contents/Resources/app/bin/codium"
}
return ""
}
if flavor == FlavorWindsurf {
if command.Exists("windsurf") {
return "windsurf"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Windsurf.app/Contents/Resources/app/bin/windsurf") {
return "/Applications/Windsurf.app/Contents/Resources/app/bin/windsurf"
}
return ""
}
if flavor == FlavorAntigravity {
if command.Exists("agy") {
return "agy"
} else if runtime.GOOS == "darwin" && command.Exists("/Applications/Antigravity.app/Contents/Resources/app/bin/antigravity") {
return "/Applications/Antigravity.app/Contents/Resources/app/bin/antigravity"
}
return ""
}
return ""
}