-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathsteps.go
More file actions
326 lines (285 loc) · 9.83 KB
/
Copy pathsteps.go
File metadata and controls
326 lines (285 loc) · 9.83 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
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
package hello
import (
"fmt"
"time"
"github.com/brevdev/brev-cli/pkg/config"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/brevdev/brev-cli/pkg/util"
"github.com/briandowns/spinner"
"github.com/fatih/color"
)
const DefaultDevEnvName = "first-workspace-react"
const spinnerSuffix = "🎉 you did it!"
func GetTextBasedONStatus(status string, t *terminal.Terminal) string {
s := ""
switch status {
case "RUNNING":
case "DEPLOYING":
s += t.Yellow("Your instance is deploying.")
s += "\nPlease wait for it to finish deploying then run " + t.Yellow("brev hello") + " to resume this walk through when your instance is ready\n"
case "UNHEALTHY":
s += t.Red("Your instance seems stuck. Can you reach out to support?")
s += "\nMessage us "
s += "\n\t in discord 👉 " + t.Yellow("https://discord.gg/RpszWaJFRA")
s += "\n\t via text or call 👉 " + t.Yellow("(415) 237-2247\n")
s += "\n\nRun " + t.Yellow("brev hello") + " to resume this walk through when your instance is ready\n"
case "STOPPED":
s += t.Yellow("Your instance is stopped.")
s += "\nRun this in your terminal to start it 👉 " + t.Yellow("brev start %s", DefaultDevEnvName)
s += "\n\nRun " + t.Yellow("brev hello") + " to resume this walk through when your instance is ready\n"
case "STOPPING":
s += t.Yellow("Your instance is stopped.")
s += "\nRun this in your terminal to start it 👉 " + t.Yellow("brev start %s", DefaultDevEnvName)
s += "\n\nRun " + t.Yellow("brev hello") + " to resume this walk through when your instance is ready\n"
default:
s += t.Red("Please create a running instance for this walk through. ")
s += "\n\tYou can do that here: " + t.Yellow(fmt.Sprintf("%s/environments/new", config.GlobalConfig.GetConsoleURL()))
s += "\n\nRun " + t.Yellow("brev hello") + " to resume this walk through when your instance is ready\n"
}
return s
}
/*
Return nil to exit the onboarding
*/
func GetDevEnvOrStall(t *terminal.Terminal, workspaces []entity.Workspace) *entity.Workspace {
var runningDevEnvs []entity.Workspace
noneFound := true
for _, v := range workspaces {
if v.Status == "RUNNING" {
noneFound = false
runningDevEnvs = append(runningDevEnvs, v)
}
}
if noneFound {
s := t.Red("Please create a running instance for this walk through. ")
s += "\n\tYou can do that here: " + t.Yellow(fmt.Sprintf("%s/environments/new", config.GlobalConfig.GetConsoleURL()))
s += "\n\nRun: " + t.Yellow("brev hello") + " to resume this walk through when your instance is ready\n"
TypeItToMe(s)
return nil
}
msg := GetTextBasedONStatus(runningDevEnvs[0].Status, t)
if msg != "" {
TypeItToMe(msg)
}
return &runningDevEnvs[0]
}
func printLsIntroText(t *terminal.Terminal, _ entity.Workspace) {
s := "\nThe command " + t.Yellow("brev ls") + " shows your instances"
s += "\nIf the instance is " + t.Green("RUNNING") + ", you can open it."
TypeItToMe(s)
}
func printBrevShellOnboarding(t *terminal.Terminal, firstWorkspace *entity.Workspace) {
s := "\n\nTry opening a terminal SSHed in your instance"
s += "\nIn a new terminal, run " + t.Green("brev shell %s", firstWorkspace.Name) + "\n"
TypeItToMe(s)
}
func printAskInstallVsCode(t *terminal.Terminal) {
// The error here is most likely because code isn't in path and we depend on that
// TODO: remove the dependency on code being in path
s := t.Yellow("\n\nCould you please install the following VSCode extension? %s", t.Green("ms-vscode-remote.remote-ssh"))
s += "\nDo that then run " + t.Yellow("brev hello") + " to resume this walk-through\n"
// s += "Here's a video of me installing the VS Code extension 👉 " + ""
TypeItToMe(s)
}
func printBrevOpen(t *terminal.Terminal, firstWorkspace entity.Workspace) {
s := "\n\nTry opening VS Code in your instance"
s += "\nIn a new terminal, run " + t.Green("brev open %s", firstWorkspace.Name) + "\n"
TypeItToMe(s)
}
func printCompletedOnboarding(t *terminal.Terminal) {
s := "\n\nI think I'm done here. Now you know how to open an instance and start coding."
s += "\n\nUse the console " + t.Yellow(fmt.Sprintf("(%s)", config.GlobalConfig.GetConsoleURL())) + " to create a new instance or share it with people"
s += "\nand use this CLI to code the way you would normally 🤙"
s += "\n\nCheck out the docs at " + t.Yellow("https://brev.dev") + " and let us know if we can help!\n"
s += "\n\nIn case you missed it, my cell is " + t.Yellow("(415) 237-2247") + "\n\t-Nader\n"
TypeItToMe(s)
}
// func waitSpinner(spinner *spinner.Spinner) error {
// // a while loop in golang
// sum := 0
// spinner.Suffix = "👆 try that, I'll wait"
// spinner.Start()
// for sum > -1 {
// sum++
// res, err2 := GetOnboardingObject()
// if err2 != nil {
// return breverrors.WrapAndTrace(err2)
// }
// if res.HasRunBrevShell {
// spinner.Suffix = spinnerSuffix
// time.Sleep(250 * time.Millisecond)
// spinner.Stop()
// break
// }
// time.Sleep(1 * time.Second)
// }
// return nil
// }
/*
Step 1:
The user just ran brev ls
*/
func Step1(t *terminal.Terminal, workspaces []entity.Workspace, user *entity.User, store HelloStore) error {
err := CompletedOnboardingLs(user, store)
if err != nil {
return breverrors.WrapAndTrace(err)
}
spinner := t.NewSpinner()
bold := color.New(color.Bold).SprintFunc()
firstWorkspace := GetDevEnvOrStall(t, workspaces)
if firstWorkspace == nil {
return nil
}
printLsIntroText(t, *firstWorkspace)
// Check if VS Code is preferred editor
currentOnboardingStatus, err := user.GetOnboardingData()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if currentOnboardingStatus.Editor == "VSCode" {
err = doVsCodeOnboarding(t, firstWorkspace, user, store, spinner, bold)
if err != nil {
return breverrors.WrapAndTrace(err)
}
} else {
err = doBrevShellOnboarding(t, firstWorkspace, user, store, spinner, bold)
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
// err = waitSpinner(spinner)
// if err != nil {
// return breverrors.WrapAndTrace(err)
// }
// err = CompletedOnboardingShell(user, store)
// if err != nil {
// return breverrors.WrapAndTrace(err)
// }
// TypeItToMe("\nHit " + t.Yellow("enter") + " to continue")
// fmt.Println()
// _ = terminal.PromptGetInput(terminal.PromptContent{
// // Label: " " + bold("▸") + " Press " + bold("Enter") + " to continue",
// Label: " " + bold("▸"),
// ErrorMsg: "error",
// AllowEmpty: true,
// })
// Commenting out the below since public urls is gone
// handleLocalhostURLIfDefaultProject(*firstWorkspace, t)
printCompletedOnboarding(t)
err = CompletedOnboarding(user, store)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
// func handleLocalhostURLIfDefaultProject(ws entity.Workspace, t *terminal.Terminal) {
// if ws.Name == DefaultDevEnvName {
// s := "\n\nOne last thing, since you're coding in the cloud, you can get a public URL to your localhost."
// s += "\nFrom within that Brev dev environment,\n\tRun " + t.Yellow("npm run start") + " to spin up the service"
// s += "\nThen instead of going to localhost:3000, \n\tGo to " + t.Yellow("https://3000-%s", ws.DNS)
// // TODO: Give that a shot then press enter
// bold := color.New(color.Bold).SprintFunc()
// s += "\n\nGive that a shot then press enter👆:"
// TypeItToMe(s)
// fmt.Print("\n")
// _ = terminal.PromptGetInput(terminal.PromptContent{
// // Label: " " + bold("▸") + " Press " + bold("Enter") + " to continue",
// Label: " " + bold("▸"),
// ErrorMsg: "error",
// AllowEmpty: true,
// })
// fmt.Print("\n")
// }
// }
func doBrevShellOnboarding(
t *terminal.Terminal,
firstWorkspace *entity.Workspace,
user *entity.User,
store HelloStore,
spinner *spinner.Spinner,
bold func(a ...interface{}) string,
) error {
printBrevShellOnboarding(t, firstWorkspace)
// a while loop in golang
sum := 0
spinner.Suffix = "☝️ try that, I'll wait"
spinner.Start()
for sum < 1 {
sum += sum
res, err1 := GetOnboardingObject()
if err1 != nil {
return breverrors.WrapAndTrace(err1)
}
if shellOnboardingPollDone(res) {
spinner.Suffix = spinnerSuffix
time.Sleep(250 * time.Millisecond)
spinner.Stop()
break
}
time.Sleep(1 * time.Second)
}
err := CompletedOnboardingShell(user, store)
if err != nil {
return breverrors.WrapAndTrace(err)
}
TypeItToMe("\nHit " + t.Yellow("enter") + " to continue")
fmt.Println()
_ = terminal.PromptGetInput(terminal.PromptContent{
// Label: " " + bold("▸") + " Press " + bold("Enter") + " to continue",
Label: " " + bold("▸"),
ErrorMsg: "error",
AllowEmpty: true,
})
return nil
}
func doVsCodeOnboarding(
t *terminal.Terminal,
firstWorkspace *entity.Workspace,
user *entity.User,
store HelloStore,
spinner *spinner.Spinner,
bold func(a ...interface{}) string,
) error {
// TODO: check if ext is installed
isInstalled, err := util.IsVSCodeExtensionInstalled("ms-vscode-remote.remote-ssh")
if err != nil {
return breverrors.WrapAndTrace(err)
}
if !isInstalled {
printAskInstallVsCode(t)
return nil
}
printBrevOpen(t, *firstWorkspace)
sum := 0
spinner.Suffix = "☝️ try that, I'll wait"
spinner.Start()
for sum < 1 {
sum += sum
res, err1 := GetOnboardingObject()
if err1 != nil {
return breverrors.WrapAndTrace(err1)
}
if res.HasRunBrevOpen {
spinner.Suffix = spinnerSuffix
time.Sleep(250 * time.Millisecond)
spinner.Stop()
break
}
time.Sleep(1 * time.Second)
}
err = CompletedOnboardingOpen(user, store)
if err != nil {
return breverrors.WrapAndTrace(err)
}
TypeItToMe("\nHit " + t.Yellow("enter") + " to continue")
fmt.Println()
_ = terminal.PromptGetInput(terminal.PromptContent{
// Label: " " + bold("▸") + " Press " + bold("Enter") + " to continue",
Label: " " + bold("▸"),
ErrorMsg: "error",
AllowEmpty: true,
})
return nil
}