-
Notifications
You must be signed in to change notification settings - Fork 318
/
open.go
103 lines (89 loc) · 2.78 KB
/
open.go
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
package vscode
import (
"context"
"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, log log.Logger) error {
log.Infof("Starting VSCode...")
err := openViaCLI(ctx, workspace, folder, newWindow, log)
if err != nil {
log.Debugf("Error opening vscode via cli: %v", err)
} else {
return nil
}
return openViaBrowser(workspace, folder, newWindow, log)
}
func openViaBrowser(workspace, folder string, newWindow bool, log log.Logger) error {
openURL := `vscode://vscode-remote/ssh-remote+` + workspace + `.devpod/` + folder
if newWindow {
openURL += "?windowId=_blank"
}
err := open.Run(openURL)
if err != nil {
log.Debugf("Starting VSCode caused error: %v", err)
log.Errorf("Seems like you don't have Visual Studio Code installed on your computer locally. Please install VSCode via https://code.visualstudio.com/")
return err
}
return nil
}
func openViaCLI(ctx context.Context, workspace, folder string, newWindow bool, log log.Logger) error {
// try to find code cli
codePath := findCLI()
if codePath == "" {
return fmt.Errorf("couldn't find the code binary")
}
// 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
for _, str := range splitted {
if strings.TrimSpace(str) == "ms-vscode-remote.remote-ssh" {
found = true
break
}
}
// install remote-ssh extension
if !found {
args := []string{"--install-extension", "ms-vscode-remote.remote-ssh"}
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 := []string{
"--folder-uri",
"vscode-remote://ssh-remote+" + workspace + ".devpod/" + folder,
"--disable-extension",
"ms-vscode-remote.remote-containers",
}
if newWindow {
args = append(args, "--new-window")
} else {
args = append(args, "--reuse-window")
}
log.Debugf("Run vscode command %s %s", codePath, strings.Join(args, " "))
out, err = exec.CommandContext(ctx, codePath, args...).CombinedOutput()
if err != nil {
return command.WrapCommandError(out, err)
}
return nil
}
func findCLI() string {
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 ""
}