Skip to content

Commit 07851a7

Browse files
committed
remove upload vscode extensions
1 parent 0d0363d commit 07851a7

File tree

5 files changed

+71
-199
lines changed

5 files changed

+71
-199
lines changed

pkg/cmd/hello/steps.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ func doVsCodeOnboarding(
288288

289289
printBrevOpen(t, *firstWorkspace)
290290

291-
// a while loop in golang
292291
sum := 0
293292
spinner.Suffix = "☝️ try that, I'll wait"
294293
spinner.Start()

pkg/cmd/login/login.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,6 @@ func (o LoginOptions) RunLogin(t *terminal.Terminal, loginToken string, skipBrow
191191
return breverrors.WrapAndTrace(err)
192192
}
193193

194-
ob, err := user.GetOnboardingData()
195-
if err != nil {
196-
return breverrors.WrapAndTrace(err)
197-
}
198-
199-
// if user chooses vscode as editor preference, import ide config
200-
if ob.Editor == "VSCode" {
201-
err = importideconfig.RunImportIDEConfig(t, o.LoginStore)
202-
if err != nil {
203-
if !strings.Contains(err.Error(), "no vscode extensions found") {
204-
return breverrors.WrapAndTrace(err)
205-
}
206-
}
207-
}
208-
209194
return nil
210195
}
211196

@@ -360,8 +345,7 @@ func OnboardUserWithEditors(t *terminal.Terminal, _ LoginStore, ide string) (str
360345
// If we couldn't check for the extension being installed, they likely don't have code in path and this step should be skipped
361346
if !isInstalled && err == nil {
362347
// attempt to install the extension
363-
cmd := exec.Command("code", "--install-extension", "ms-vscode-remote.remote-ssh", "--force") // #nosec G204
364-
_ = cmd.Run()
348+
_ = util.InstallVscodeExtension("ms-vscode-remote.remote-ssh")
365349

366350
// verify installation
367351
isInstalled, err := util.IsVSCodeExtensionInstalled("ms-vscode-remote.remote-ssh")

pkg/cmd/open/open.go

Lines changed: 19 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
breverrors "github.com/brevdev/brev-cli/pkg/errors"
1919
"github.com/brevdev/brev-cli/pkg/store"
2020
"github.com/brevdev/brev-cli/pkg/terminal"
21+
uutil "github.com/brevdev/brev-cli/pkg/util"
2122
"github.com/brevdev/brev-cli/pkg/writeconnectionevent"
2223
"github.com/briandowns/spinner"
2324
"github.com/hashicorp/go-multierror"
24-
"github.com/samber/lo"
2525
"github.com/samber/mo"
2626

2727
"github.com/spf13/cobra"
@@ -227,9 +227,21 @@ func openVsCodeWithSSH(
227227

228228
// todo: add it here
229229
s.Suffix = " Instance is ready. Opening VS Code 🤙"
230-
time.Sleep(400 * time.Millisecond)
230+
time.Sleep(250 * time.Millisecond)
231231
s.Stop()
232232
t.Vprintf("\n")
233+
234+
isRemoteInstalled, err := uutil.IsVSCodeExtensionInstalled("ms-vscode-remote.remote-ssh")
235+
if err != nil {
236+
return breverrors.WrapAndTrace(err)
237+
}
238+
if !isRemoteInstalled {
239+
err = uutil.InstallVscodeExtension("ms-vscode-remote.remote-ssh")
240+
if err != nil {
241+
return breverrors.WrapAndTrace(err)
242+
}
243+
}
244+
233245
err = openVsCode(sshAlias, path, tstore)
234246

235247
// check if we are in a brev environment, if so transform the error message
@@ -289,110 +301,20 @@ type vscodePathStore interface {
289301
GetWindowsDir() (string, error)
290302
}
291303

292-
var commonVSCodePaths = lo.Map([]string{
293-
"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code",
294-
"/usr/bin/code",
295-
"/usr/local/bin/code",
296-
"/snap/bin/code",
297-
"/usr/local/share/code/bin/code",
298-
"/usr/share/code/bin/code",
299-
"/usr/share/code-insiders/bin/code-insiders",
300-
"/usr/share/code-oss/bin/code-oss",
301-
"/usr/share/code/bin/code",
302-
},
303-
func(path string, _ int) mo.Result[string] {
304-
return mo.Ok(path)
305-
})
306-
307-
func getCommonVsCodePaths(store vscodePathStore) []mo.Result[string] {
308-
paths := commonVSCodePaths
309-
paths = append(paths, []mo.Result[string]{
310-
// not dry but it's a one off
311-
mo.TupleToResult(store.GetWindowsDir()).Match(
312-
func(dir string) (string, error) {
313-
return fmt.Sprintf(
314-
"%s/AppData/Local/Programs/Microsoft VS Code/Code.exe",
315-
dir,
316-
), nil
317-
},
318-
func(err error) (string, error) {
319-
return "", breverrors.WrapAndTrace(err) // no windows dir
320-
},
321-
),
322-
mo.TupleToResult(store.GetWindowsDir()).Match(
323-
func(dir string) (string, error) {
324-
return fmt.Sprintf(
325-
"%s/AppData/Local/Programs/Microsoft VS Code/bin/code",
326-
dir,
327-
), nil
328-
},
329-
func(err error) (string, error) {
330-
return "", breverrors.WrapAndTrace(err) // no windows dir
331-
},
332-
),
333-
}...)
304+
func getWindowsVsCodePaths(store vscodePathStore) []string {
305+
wd, _ := store.GetWindowsDir()
306+
paths := append([]string{}, fmt.Sprintf("%s/AppData/Local/Programs/Microsoft VS Code/Code.exe", wd), fmt.Sprintf("%s/AppData/Local/Programs/Microsoft VS Code/bin/code", wd))
334307
return paths
335308
}
336309

337-
func tryToOpenVsCodeViaExecutable(sshAlias, path string, vscodepaths []mo.Result[string]) error {
338-
errs := multierror.Append(nil)
339-
for _, vscodepath := range vscodepaths {
340-
err := openVsCodeViaExecutable(sshAlias, path, vscodepath)
341-
if err != nil {
342-
errs = multierror.Append(errs, err)
343-
} else {
344-
return breverrors.WrapAndTrace(errs.ErrorOrNil())
345-
}
346-
}
347-
return breverrors.WrapAndTrace(errs.ErrorOrNil())
348-
}
349-
350310
func openVsCode(sshAlias string, path string, store OpenStore) error {
351311
vscodeString := fmt.Sprintf("vscode-remote://ssh-remote+%s%s", sshAlias, path)
352312
vscodeString = shellescape.QuoteCommand([]string{vscodeString})
353-
cmd := exec.Command("code", "--folder-uri", vscodeString) // #nosec G204
354-
err := cmd.Run()
355-
if err != nil {
356-
vscodepaths := getCommonVsCodePaths(store)
357-
err := tryToOpenVsCodeViaExecutable(sshAlias, path, vscodepaths)
358-
if err != nil {
359-
return breverrors.WrapAndTrace(err)
360-
}
361-
}
362-
return nil
363-
}
364313

365-
func openVsCodeViaExecutable(sshAlias, path string, vscodepath mo.Result[string]) error {
366-
err := vscodepath.Match(
367-
func(vscodepath string) (string, error) {
368-
vscodeString := fmt.Sprintf("vscode-remote://ssh-remote+%s%s", sshAlias, path)
369-
vscodeString = shellescape.QuoteCommand([]string{vscodeString})
370-
cmd := exec.Command(vscodepath, "--folder-uri", vscodeString) // #nosec G204
371-
err := cmd.Run()
372-
if err != nil {
373-
return "", breverrors.WrapAndTrace(err)
374-
}
375-
return "", nil
376-
},
377-
func(err error) (string, error) {
378-
return "", breverrors.WrapAndTrace(err)
379-
},
380-
).Error()
314+
windowsPaths := getWindowsVsCodePaths(store)
315+
_, err := uutil.TryRunVsCodeCommand([]string{"--folder-uri", vscodeString}, windowsPaths...)
381316
if err != nil {
382317
return breverrors.WrapAndTrace(err)
383318
}
384319
return nil
385320
}
386-
387-
// func showLogsToUserIfTheyPressEnter(sshAlias string, showLogsToUser *bool, s *spinner.Spinner) {
388-
// scanner := bufio.NewScanner(os.Stdin)
389-
// for scanner.Scan() {
390-
// *showLogsToUser = true
391-
// out, err := exec.Command("ssh", "-o", "RemoteCommand=none", sshAlias, "cat", "/var/log/brev-workspace.log").CombinedOutput()
392-
// fmt.Print(string(out))
393-
// if err != nil {
394-
// fmt.Println(err)
395-
// }
396-
// s.Suffix = ""
397-
// }
398-
// }

pkg/cmd/open/open_test.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1 @@
11
package open
2-
3-
import (
4-
"errors"
5-
"testing"
6-
7-
"github.com/google/go-cmp/cmp"
8-
"github.com/samber/mo"
9-
)
10-
11-
type mockVscodePathStore struct{}
12-
13-
func (m *mockVscodePathStore) GetWindowsDir() (string, error) {
14-
return "/mnt/c/Users/1234", nil
15-
}
16-
17-
type mockVscodePathStoreAlwaysError struct{}
18-
19-
func (m *mockVscodePathStoreAlwaysError) GetWindowsDir() (string, error) {
20-
return "", errors.New("error")
21-
}
22-
23-
func Test_getCommonVsCodePaths(t *testing.T) {
24-
type args struct {
25-
store vscodePathStore
26-
}
27-
tests := []struct {
28-
name string
29-
args args
30-
want []mo.Result[string]
31-
}{
32-
// TODO: Add test cases.
33-
{
34-
name: "test",
35-
args: args{
36-
store: &mockVscodePathStore{},
37-
},
38-
want: append(
39-
commonVSCodePaths,
40-
[]mo.Result[string]{
41-
mo.Ok("/mnt/c/Users/1234/AppData/Local/Programs/Microsoft VS Code/Code.exe"),
42-
mo.Ok("/mnt/c/Users/1234/AppData/Local/Programs/Microsoft VS Code/bin/code"),
43-
}...),
44-
},
45-
{
46-
name: "test",
47-
args: args{
48-
store: &mockVscodePathStoreAlwaysError{},
49-
},
50-
want: append(
51-
commonVSCodePaths,
52-
[]mo.Result[string]{
53-
mo.Err[string](errors.New("error")),
54-
mo.Err[string](errors.New("error")),
55-
}...),
56-
},
57-
}
58-
for _, tt := range tests {
59-
t.Run(tt.name, func(t *testing.T) {
60-
got := getCommonVsCodePaths(tt.args.store)
61-
diff := cmp.Diff(tt.want, got,
62-
cmp.AllowUnexported(mo.Result[string]{}),
63-
cmp.Comparer(func(x, y mo.Result[string]) bool {
64-
if x.IsOk() && y.IsOk() {
65-
return x.MustGet() == y.MustGet()
66-
}
67-
if x.IsError() && y.IsError() {
68-
return true
69-
}
70-
return false
71-
}),
72-
)
73-
if diff != "" {
74-
t.Errorf("getCommonVsCodePaths() mismatch (-want +got):\n%s", diff)
75-
}
76-
})
77-
}
78-
}

pkg/util/util.go

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
breverrors "github.com/brevdev/brev-cli/pkg/errors"
1212

1313
"github.com/hashicorp/go-multierror"
14-
"golang.org/x/text/encoding/charmap"
1514
)
1615

1716
// This package should only be used as a holding pattern to be later moved into more specific packages
@@ -92,17 +91,62 @@ func DoesPathExist(path string) bool {
9291
return false
9392
}
9493

95-
func IsVSCodeExtensionInstalled(extensionID string) (bool, error) {
96-
cmdddd := exec.Command("code", "--list-extensions") // #nosec G204
97-
in, err := cmdddd.Output()
94+
func InstallVscodeExtension(extensionID string) error {
95+
_, err := TryRunVsCodeCommand([]string{"--install-extension", extensionID, "--force"})
9896
if err != nil {
99-
return false, breverrors.WrapAndTrace(err)
97+
return breverrors.WrapAndTrace(err)
10098
}
99+
return nil
100+
}
101101

102-
d := charmap.CodePage850.NewDecoder()
103-
out, err := d.Bytes(in)
102+
func IsVSCodeExtensionInstalled(extensionID string) (bool, error) {
103+
out, err := TryRunVsCodeCommand([]string{"--list-extensions"})
104104
if err != nil {
105105
return false, breverrors.WrapAndTrace(err)
106106
}
107107
return strings.Contains(string(out), extensionID), nil
108108
}
109+
110+
func TryRunVsCodeCommand(args []string, extraPaths ...string) ([]byte, error) {
111+
extraPaths = append(commonVSCodePaths, extraPaths...)
112+
out, err := runManyVsCodeCommand(extraPaths, args)
113+
if err != nil {
114+
return nil, breverrors.WrapAndTrace(err)
115+
}
116+
return out, nil
117+
}
118+
119+
func runManyVsCodeCommand(vscodepaths []string, args []string) ([]byte, error) {
120+
errs := multierror.Append(nil)
121+
for _, vscodepath := range vscodepaths {
122+
out, err := runVsCodeCommand(vscodepath, args)
123+
if err != nil {
124+
errs = multierror.Append(errs, err)
125+
} else {
126+
return out, nil
127+
}
128+
}
129+
return nil, breverrors.WrapAndTrace(errs.ErrorOrNil())
130+
}
131+
132+
func runVsCodeCommand(vscodepath string, args []string) ([]byte, error) {
133+
cmd := exec.Command(vscodepath, args...) // #nosec G204
134+
res, err := cmd.CombinedOutput()
135+
if err != nil {
136+
return nil, breverrors.WrapAndTrace(err)
137+
}
138+
return res, nil
139+
}
140+
141+
var commonVSCodePaths = []string{
142+
"code",
143+
"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code",
144+
"/usr/bin/code",
145+
"/usr/local/bin/code",
146+
"/snap/bin/code",
147+
"/usr/local/share/code/bin/code",
148+
"/usr/share/code/bin/code",
149+
"/usr/share/code-insiders/bin/code-insiders",
150+
"/usr/share/code-oss/bin/code-oss",
151+
"/usr/share/code/bin/code",
152+
}

0 commit comments

Comments
 (0)