generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
open.go
337 lines (302 loc) · 10.5 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package open
import (
"errors"
"fmt"
"os/exec"
"strings"
"time"
"github.com/alessio/shellescape"
"github.com/brevdev/brev-cli/pkg/analytics"
"github.com/brevdev/brev-cli/pkg/cmd/cmderrors"
"github.com/brevdev/brev-cli/pkg/cmd/completions"
"github.com/brevdev/brev-cli/pkg/cmd/hello"
"github.com/brevdev/brev-cli/pkg/cmd/refresh"
"github.com/brevdev/brev-cli/pkg/cmd/util"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
uutil "github.com/brevdev/brev-cli/pkg/util"
"github.com/brevdev/brev-cli/pkg/writeconnectionevent"
"github.com/briandowns/spinner"
"github.com/hashicorp/go-multierror"
"github.com/samber/mo"
"github.com/spf13/cobra"
)
var (
openLong = "[command in beta] This will open VS Code SSH-ed in to your workspace. You must have 'code' installed in your path."
openExample = "brev open workspace_id_or_name\nbrev open my-app\nbrev open h9fp5vxwe"
)
type OpenStore interface {
util.GetWorkspaceByNameOrIDErrStore
refresh.RefreshStore
UpdateUser(string, *entity.UpdateUser) (*entity.User, error)
GetOrganizations(options *store.GetOrganizationsOptions) ([]entity.Organization, error)
GetWorkspaces(organizationID string, options *store.GetWorkspacesOptions) ([]entity.Workspace, error)
StartWorkspace(workspaceID string) (*entity.Workspace, error)
GetActiveOrganizationOrDefault() (*entity.Organization, error)
GetWorkspace(workspaceID string) (*entity.Workspace, error)
GetWindowsDir() (string, error)
IsWorkspace() (bool, error)
}
func NewCmdOpen(t *terminal.Terminal, store OpenStore, noLoginStartStore OpenStore) *cobra.Command {
var waitForSetupToFinish bool
var directory string
cmd := &cobra.Command{
Annotations: map[string]string{"ssh": ""},
Use: "open",
DisableFlagsInUseLine: true,
Short: "[beta] open VSCode to ",
Long: openLong,
Example: openExample,
Args: cmderrors.TransformToValidationError(cobra.ExactArgs(1)),
ValidArgsFunction: completions.GetAllWorkspaceNameCompletionHandler(noLoginStartStore, t),
RunE: func(cmd *cobra.Command, args []string) error {
setupDoneString := "------ Git repo cloned ------"
if waitForSetupToFinish {
setupDoneString = "------ Done running execs ------"
}
err := runOpenCommand(t, store, args[0], setupDoneString, directory)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}
cmd.Flags().BoolVarP(&waitForSetupToFinish, "wait", "w", false, "wait for setup to finish")
cmd.Flags().StringVarP(&directory, "dir", "d", "", "directory to open")
return cmd
}
// Fetch workspace info, then open code editor
func runOpenCommand(t *terminal.Terminal, tstore OpenStore, wsIDOrName string, setupDoneString string, directory string) error {
// todo check if workspace is stopped and start if it if it is stopped
fmt.Println("finding your instance...")
res := refresh.RunRefreshAsync(tstore)
workspace, err := util.GetUserWorkspaceByNameOrIDErr(tstore, wsIDOrName)
if err != nil {
return breverrors.WrapAndTrace(err)
}
if workspace.Status == "STOPPED" { // we start the env for the user
err = startWorkspaceIfStopped(t, tstore, wsIDOrName, workspace)
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
err = pollUntil(t, workspace.ID, "RUNNING", tstore)
if err != nil {
return breverrors.WrapAndTrace(err)
}
workspace, err = util.GetUserWorkspaceByNameOrIDErr(tstore, wsIDOrName)
if err != nil {
return breverrors.WrapAndTrace(err)
}
if workspace.Status != "RUNNING" {
return breverrors.WorkspaceNotRunning{Status: workspace.Status}
}
projPath := workspace.GetProjectFolderPath()
if directory != "" {
projPath = directory
}
localIdentifier := workspace.GetLocalIdentifier()
err = res.Await()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = hello.SetHasRunOpen(true)
if err != nil {
return breverrors.WrapAndTrace(err)
}
// we don't care about the error here but should log with sentry
// legacy environments wont support this and cause errrors,
// but we don't want to block the user from using vscode
_ = writeconnectionevent.WriteWCEOnEnv(tstore, string(localIdentifier))
err = openVsCodeWithSSH(t, string(localIdentifier), projPath, tstore, setupDoneString)
if err != nil {
if strings.Contains(err.Error(), `"code": executable file not found in $PATH`) {
errMsg := "code\": executable file not found in $PATH\n\nadd 'code' to your $PATH to open VS Code from the terminal\n\texport PATH=\"/Applications/Visual Studio Code.app/Contents/Resources/app/bin:$PATH\""
_, errStore := tstore.UpdateUser(
workspace.CreatedByUserID,
&entity.UpdateUser{
OnboardingData: map[string]interface{}{
"pathErrorTS": time.Now().UTC().Unix(),
},
})
if errStore != nil {
return errors.New(errMsg + "\n" + errStore.Error())
}
return errors.New(errMsg)
}
return breverrors.WrapAndTrace(err)
}
// Call analytics for open
_ = pushOpenAnalytics(tstore, workspace)
return nil
}
func pushOpenAnalytics(tstore OpenStore, workspace *entity.Workspace) error {
userID := ""
user, err := tstore.GetCurrentUser()
if err != nil {
userID = workspace.CreatedByUserID
} else {
userID = user.ID
}
data := analytics.EventData{
EventName: "Brev Open",
UserID: userID,
Properties: map[string]string{
"instanceId": workspace.ID,
},
}
err = analytics.TrackEvent(data)
return breverrors.WrapAndTrace(err)
}
func pollUntil(t *terminal.Terminal, wsid string, state string, openStore OpenStore) error {
s := t.NewSpinner()
isReady := false
s.Suffix = " hang tight 🤙"
s.Start()
for !isReady {
time.Sleep(5 * time.Second)
ws, err := openStore.GetWorkspace(wsid)
if err != nil {
return breverrors.WrapAndTrace(err)
}
s.Suffix = " workspace is currently " + strings.ToLower(ws.Status)
if ws.Status == state {
s.Suffix = "Workspace is ready!"
s.Stop()
isReady = true
}
}
return nil
}
func startWorkspaceIfStopped(t *terminal.Terminal, tstore OpenStore, wsIDOrName string, workspace *entity.Workspace) error {
startedWorkspace, err := tstore.StartWorkspace(workspace.ID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
t.Vprintf(t.Yellow("Instance %s is starting. \n\n", startedWorkspace.Name))
workspace, err = util.GetUserWorkspaceByNameOrIDErr(tstore, wsIDOrName)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func tryToInstallExtensions(
t *terminal.Terminal,
extIDs []string,
) {
for _, extID := range extIDs {
extInstalled, err0 := uutil.IsVSCodeExtensionInstalled(extID)
if !extInstalled {
err1 := uutil.InstallVscodeExtension(extID)
isRemoteInstalled, err2 := uutil.IsVSCodeExtensionInstalled(extID)
if !isRemoteInstalled {
err := multierror.Append(err0, err1, err2)
t.Print(t.Red("Couldn't install the necessary VSCode extension automatically.\nError: " + err.Error()))
t.Print("\tPlease install VSCode and the following VSCode extension: " + t.Yellow(extID) + ".\n")
_ = terminal.PromptGetInput(terminal.PromptContent{
Label: "Hit enter when finished:",
ErrorMsg: "error",
AllowEmpty: true,
})
}
}
}
}
// Opens code editor. Attempts to install code in path if not installed already
func openVsCodeWithSSH(
t *terminal.Terminal,
sshAlias string,
path string,
tstore OpenStore,
_ string,
) error {
// infinite for loop:
res := refresh.RunRefreshAsync(tstore)
err := res.Await()
if err != nil {
return breverrors.WrapAndTrace(err)
}
s := t.NewSpinner()
s.Start()
s.Suffix = " checking if your instance is ready..."
err = waitForSSHToBeAvailable(t, s, sshAlias)
if err != nil {
return breverrors.WrapAndTrace(err)
}
// todo: add it here
s.Suffix = " Instance is ready. Opening VS Code 🤙"
time.Sleep(250 * time.Millisecond)
s.Stop()
t.Vprintf("\n")
tryToInstallExtensions(t, []string{"ms-vscode-remote.remote-ssh", "ms-toolsai.jupyter-keymap", "ms-python.python"})
err = openVsCode(sshAlias, path, tstore)
if err != nil {
return breverrors.WrapAndTrace(err)
}
// check if we are in a brev environment, if so transform the error message
// to indicate that the user should run brev open locally instead of in
// the cloud and that we intend on supporting this in the future
// if there is an error getting the workspace, append that error with
// multierror,
// otherwise, just return the error
err = mo.TupleToResult(tstore.IsWorkspace()).Match(
func(value bool) (bool, error) {
if value {
// todo log original error to sentry
return true, errors.New("you are in a remote brev instance; brev open is not supported. Please run brev open locally instead")
}
return false, breverrors.WrapAndTrace(err)
},
func(err2 error) (bool, error) {
return false, multierror.Append(err, err2)
},
).Error()
if err != nil {
if strings.Contains(err.Error(), "you are in a remote brev instance;") {
return breverrors.WrapAndTrace(err)
}
return breverrors.WrapAndTrace(fmt.Errorf(t.Red("couldn't open VSCode, try adding it to PATH (you can do this in VSCode by running CMD-SHIFT-P and typing 'install code in path')\n")))
} else {
return nil
}
}
func waitForSSHToBeAvailable(t *terminal.Terminal, s *spinner.Spinner, sshAlias string) error {
counter := 0
for {
cmd := exec.Command("ssh", "-o", "ConnectTimeout=1", sshAlias, "echo", " ")
out, err := cmd.CombinedOutput()
if err == nil {
return nil
}
outputStr := string(out)
stdErr := strings.Split(outputStr, "\n")[1]
if counter == 160 || !store.SatisfactorySSHErrMessage(stdErr) {
return breverrors.WrapAndTrace(errors.New("\n" + stdErr))
}
if counter == 10 {
s.Suffix = t.Green(" waiting for SSH connection to be available 🤙")
}
counter++
time.Sleep(1 * time.Second)
}
}
type vscodePathStore interface {
GetWindowsDir() (string, error)
}
func getWindowsVsCodePaths(store vscodePathStore) []string {
wd, _ := store.GetWindowsDir()
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))
return paths
}
func openVsCode(sshAlias string, path string, store OpenStore) error {
vscodeString := fmt.Sprintf("vscode-remote://ssh-remote+%s%s", sshAlias, path)
vscodeString = shellescape.QuoteCommand([]string{vscodeString})
windowsPaths := getWindowsVsCodePaths(store)
_, err := uutil.TryRunVsCodeCommand([]string{"--folder-uri", vscodeString}, windowsPaths...)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}