-
Notifications
You must be signed in to change notification settings - Fork 542
Expand file tree
/
Copy pathopen.go
More file actions
206 lines (181 loc) · 6.38 KB
/
open.go
File metadata and controls
206 lines (181 loc) · 6.38 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
package vscode
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"runtime"
"slices"
"strings"
"github.com/loft-sh/devpod/pkg/command"
"github.com/loft-sh/log"
"github.com/skratchdot/open-golang/open"
)
const (
FlatpakStable string = "com.visualstudio.code"
FlatpakInsiders string = "com.visualstudio.code.insiders"
FlatpakCodium string = "com.vscodium.codium"
FlatpakCodiumInsiders string = "com.vscodium.codium-insiders"
)
func Open(ctx context.Context, workspace, folder string, newWindow bool, flavor Flavor, sshConfigPath string, log log.Logger) error {
log.Infof("Starting %s...", flavor.DisplayName())
cliErr := openViaCLI(ctx, workspace, folder, newWindow, flavor, sshConfigPath, 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 {
protocol := `vscode://`
switch flavor {
case FlavorStable:
protocol = `vscode://`
case FlavorInsiders:
protocol = `vscode-insiders://`
case FlavorCursor:
protocol = `cursor://`
case FlavorPositron:
protocol = `positron://`
case FlavorCodium:
protocol = `codium://`
case FlavorCodiumInsiders:
protocol = `codium-insiders://`
}
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)
return err
}
return nil
}
func openViaCLI(ctx context.Context, workspace, folder string, newWindow bool, flavor Flavor, sshConfigPath string, log log.Logger) error {
// try to find code cli
codePath := findCLI(flavor, log)
if codePath == nil {
return fmt.Errorf("couldn't find the %s binary", flavor)
}
if codePath[0] == "flatpak" {
log.Debugf("Running with Flatpak using the package %s.", codePath[2])
out, err := exec.Command(codePath[0], "ps", "--columns=application").Output()
if err != nil {
return command.WrapCommandError(out, err)
}
splitted := strings.Split(string(out), "\n")
foundRunning := false
// Ignore the header
for _, str := range splitted[1:] {
if strings.TrimSpace(str) == codePath[2] {
foundRunning = true
break
}
}
if foundRunning {
log.Warnf("The IDE is already running via Flatpak. If you are encountering SSH connectivity issues, make sure to give read access to your SSH config file (e.g flatpak override %s --filesystem=%s) and restart your IDE.", codePath[2], sshConfigPath)
}
codePath = slices.Insert(codePath, 2, fmt.Sprintf("--filesystem=%s:ro", sshConfigPath))
}
sshExtension := "ms-vscode-remote.remote-ssh"
if flavor == FlavorCodium || flavor == FlavorCodiumInsiders {
sshExtension = "jeanp413.open-remote-ssh"
}
// make sure ms-vscode-remote.remote-ssh is installed
listArgs := append(codePath, "--list-extensions")
out, err := exec.Command(listArgs[0], listArgs[1:]...).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 := append(codePath, "--install-extension", sshExtension)
log.Debugf("Run vscode command %s %s", args[0], strings.Join(args[1:], " "))
out, err := exec.CommandContext(ctx, args[0], args[1:]...).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(codePath, args...)
args = append(args, folderUriArg)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
var b bytes.Buffer
if codePath[0] != "flatpak" {
cmd.Stdout = &b
cmd.Stderr = &b
} else {
log.Debug("Skipping output capture due to issue with `flatpak run` leading the command to hang")
}
log.Debugf("Run %s command %s %s", flavor.DisplayName(), args[0], strings.Join(args[1:], " "))
err = cmd.Run()
if err != nil {
return command.WrapCommandError(b.Bytes(), err)
}
return nil
}
func existsInFlatpak(packageName string, log log.Logger) bool {
if err := exec.Command("flatpak", "info", packageName).Run(); err == nil {
return true
} else {
log.Debugf("Flatpak command for %s returned: %s", packageName, err)
}
return false
}
func getCommandArgs(execName, macOSPath, flatpakPackage string, log log.Logger) []string {
if command.Exists(execName) {
return []string{execName}
}
if runtime.GOOS == "darwin" && command.Exists(macOSPath) {
return []string{macOSPath}
}
if runtime.GOOS == "linux" && flatpakPackage != "" && command.Exists("flatpak") && existsInFlatpak(flatpakPackage, log) {
return []string{"flatpak", "run", flatpakPackage}
}
return nil
}
func findCLI(flavor Flavor, log log.Logger) []string {
switch flavor {
case FlavorStable:
return getCommandArgs("code", "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code", FlatpakStable, log)
case FlavorInsiders:
return getCommandArgs("code-insiders", "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code", FlatpakInsiders, log)
case FlavorCursor:
return getCommandArgs("cursor", "/Applications/Cursor.app/Contents/Resources/app/bin/cursor", "", log)
case FlavorPositron:
return getCommandArgs("positron", "/Applications/Positron.app/Contents/Resources/app/bin/positron", "", log)
case FlavorCodium:
return getCommandArgs("codium", "/Applications/Codium.app/Contents/Resources/app/bin/codium", FlatpakCodium, log)
case FlavorCodiumInsiders:
return getCommandArgs("codium-insiders", "/Applications/CodiumInsiders.app/Contents/Resources/app/bin/codium-insiders", FlatpakCodiumInsiders, log)
}
return nil
}