-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathvscode.go
More file actions
471 lines (412 loc) · 12.2 KB
/
vscode.go
File metadata and controls
471 lines (412 loc) · 12.2 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package vscode
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/loft-sh/devpod/pkg/command"
"github.com/loft-sh/devpod/pkg/config"
copy2 "github.com/loft-sh/devpod/pkg/copy"
"github.com/loft-sh/devpod/pkg/ide"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
OpenNewWindow = "OPEN_NEW_WINDOW"
)
type Flavor string
const (
FlavorStable Flavor = "stable"
FlavorInsiders Flavor = "insiders"
FlavorCursor Flavor = "cursor"
FlavorPositron Flavor = "positron"
FlavorCodium Flavor = "codium"
FlavorCodiumInsiders Flavor = "codium-insiders"
)
func (f Flavor) DisplayName() string {
switch f {
case FlavorStable:
return "VSCode"
case FlavorInsiders:
return "VSCode Insiders"
case FlavorCursor:
return "Cursor"
case FlavorPositron:
return "positron"
case FlavorCodium:
return "VSCodium"
default:
return "VSCode"
}
}
var Options = ide.Options{
OpenNewWindow: {
Name: OpenNewWindow,
Description: "If true, DevPod will open the project in a new window",
Default: "true",
Enum: []string{
"false",
"true",
},
},
}
func NewVSCodeServer(extensions []string, settings string, userName string, values map[string]config.OptionValue, flavor Flavor, log log.Logger) *VsCodeServer {
if flavor == "" {
flavor = FlavorStable
}
return &VsCodeServer{
values: values,
extensions: extensions,
settings: settings,
userName: userName,
log: log,
flavor: flavor,
}
}
type VsCodeServer struct {
values map[string]config.OptionValue
extensions []string
settings string
userName string
flavor Flavor
log log.Logger
}
func (o *VsCodeServer) InstallExtensions() error {
location, err := prepareServerLocation(o.userName, false, o.flavor)
if err != nil {
return err
}
binPath := o.findServerBinaryPath(location)
if binPath == "" {
return fmt.Errorf("unable to locate server binary in workspace")
}
// start log writer
writer := o.log.Writer(logrus.InfoLevel, false)
errwriter := o.log.Writer(logrus.ErrorLevel, false)
defer writer.Close()
defer errwriter.Close()
// download extensions
for _, extension := range o.extensions {
o.log.Info("Install extension " + extension + "...")
runCommand := fmt.Sprintf("%s serve-local --accept-server-license-terms --install-extension '%s'", binPath, extension)
args := []string{}
if o.userName != "" {
args = append(args, "su", o.userName, "-c", runCommand)
} else {
args = append(args, "sh", "-c", runCommand)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = writer
cmd.Stderr = errwriter
err := cmd.Run()
if err != nil {
o.log.Warn("Failed installing extension " + extension)
}
o.log.Info("Successfully installed extension " + extension)
}
return nil
}
func (o *VsCodeServer) Install() error {
location, err := prepareServerLocation(o.userName, true, o.flavor)
if err != nil {
return err
}
settingsDir := filepath.Join(location, "data", "Machine")
err = os.MkdirAll(settingsDir, 0755)
if err != nil {
return err
}
// is installed
settingsFile := filepath.Join(settingsDir, "settings.json")
_, err = os.Stat(settingsFile)
if err == nil {
return nil
}
InstallAPKRequirements(o.log)
// add settings
if o.settings == "" {
o.settings = "{}"
}
// set settings
err = os.WriteFile(settingsFile, []byte(o.settings), 0600)
if err != nil {
return err
}
// chown location
if o.userName != "" {
err = copy2.ChownR(location, o.userName)
if err != nil {
return errors.Wrap(err, "chown")
}
}
return nil
}
func (o *VsCodeServer) findServerBinaryPath(location string) string {
binPath := ""
// Limit time we spend to look for code server binary.
// Potentially expose as context option in the future if problems arise
deadline := time.Now().Add(time.Minute * 10)
if o.flavor == FlavorStable {
// check legacy location `$HOME/.vscode-server/bin`
binDir := filepath.Join(location, "bin")
for {
if time.Now().After(deadline) {
o.log.Warn("Timed out installing vscode-server")
break
}
entries, err := os.ReadDir(binDir)
if err != nil || len(entries) == 0 {
o.log.Infof("Read dir %s: %v", binDir, err)
o.log.Info("Wait until vscode-server is installed...")
// check new location `$HOME/.vscode-server/cli/servers/Stable-<version>/server/bin/code-server`
newBinPath, err := o.findCodeServerBinary(location)
if err != nil {
o.log.Infof("Read new location %s: %v", location, err)
o.log.Info("Wait until vscode-server-insiders is installed...")
time.Sleep(time.Second * 3)
continue
}
binPath = newBinPath
break
}
binPath = filepath.Join(binDir, entries[0].Name(), "bin", "code-server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
o.log.Infof("Execute %s: %v", binPath, command.WrapCommandError(out, err))
o.log.Info("Wait until vscode-server is installed...")
time.Sleep(time.Second * 3)
continue
}
break
}
return binPath
}
if o.flavor == FlavorCursor {
// check legacy location `$HOME/.cursor-server/bin`
binDir := filepath.Join(location, "bin")
for {
if time.Now().After(deadline) {
o.log.Warn("Timed out installing cursor-server")
break
}
entries, err := os.ReadDir(binDir)
if err != nil || len(entries) == 0 {
o.log.Infof("Read dir %s: %v", binDir, err)
o.log.Info("Wait until cursor-server is installed...")
// check new location `$HOME/.cursor-server/cli/servers/Stable-<version>/server/bin/cursor-server`
newBinPath, err := o.findCodeServerBinary(location)
if err != nil {
o.log.Infof("Read new location %s: %v", location, err)
o.log.Info("Wait until cursor-server is installed...")
time.Sleep(time.Second * 3)
continue
}
binPath = newBinPath
break
}
binPath = filepath.Join(binDir, entries[0].Name(), "bin", "cursor-server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
o.log.Infof("Execute %s: %v", binPath, command.WrapCommandError(out, err))
o.log.Info("Wait until cursor-server is installed...")
time.Sleep(time.Second * 3)
continue
}
break
}
return binPath
}
if o.flavor == FlavorPositron {
// check legacy location `$HOME/.positron-server/bin`
binDir := filepath.Join(location, "bin")
for {
if time.Now().After(deadline) {
o.log.Warn("Timed out installing positron-server")
break
}
entries, err := os.ReadDir(binDir)
if err != nil || len(entries) == 0 {
o.log.Infof("Read dir %s: %v", binDir, err)
o.log.Info("Wait until positron-server is installed...")
// check new location `$HOME/.positron-server/cli/servers/Stable-<version>/server/bin/positron-server`
newBinPath, err := o.findCodeServerBinary(location)
if err != nil {
o.log.Infof("Read new location %s: %v", location, err)
o.log.Info("Wait until positron-server is installed...")
time.Sleep(time.Second * 3)
continue
}
binPath = newBinPath
break
}
binPath = filepath.Join(binDir, entries[0].Name(), "bin", "positron-server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
o.log.Infof("Execute %s: %v", binPath, command.WrapCommandError(out, err))
o.log.Info("Wait until positron-server is installed...")
time.Sleep(time.Second * 3)
continue
}
break
}
return binPath
}
if o.flavor == FlavorCodium {
// check legacy location `$HOME/.vscodium-server/bin`
binDir := filepath.Join(location, "bin")
for {
if time.Now().After(deadline) {
o.log.Warn("Timed out installing vscodium-server")
break
}
entries, err := os.ReadDir(binDir)
if err != nil || len(entries) == 0 {
o.log.Infof("Read dir %s: %v", binDir, err)
o.log.Info("Wait until vscodium-server is installed...")
// check new location `$HOME/.vscodium-server/cli/servers/Stable-<version>/server/bin/code-server`
newBinPath, err := o.findCodeServerBinary(location)
if err != nil {
o.log.Infof("Read new location %s: %v", location, err)
o.log.Info("Wait until vscodium is installed...")
time.Sleep(time.Second * 3)
continue
}
binPath = newBinPath
break
}
binPath = filepath.Join(binDir, entries[0].Name(), "bin", "codium-server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
o.log.Infof("Execute %s: %v", binPath, command.WrapCommandError(out, err))
o.log.Info("Wait until vscodium-server is installed...")
time.Sleep(time.Second * 3)
continue
}
break
}
return binPath
}
if o.flavor == FlavorInsiders {
serversDir := filepath.Join(location, "cli", "servers")
for {
if time.Now().After(deadline) {
o.log.Warn("Timed out installing vscode-server-insiders")
break
}
entries, err := os.ReadDir(serversDir)
if err != nil || len(entries) == 0 {
o.log.Infof("Read dir %s: %v", serversDir, err)
o.log.Info("Wait until vscode-server-insiders is installed...")
time.Sleep(time.Second * 3)
continue
}
insidersDir := ""
// find first entry with `Insiders-` prefix
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if !strings.HasPrefix(entry.Name(), "Insiders-") {
continue
}
insidersDir = filepath.Join(serversDir, entry.Name())
}
if insidersDir == "" {
o.log.Infof("Read dir %s: install dir is missing", serversDir)
o.log.Infof("Wait until vscode-server-insiders is installed...")
time.Sleep(time.Second * 3)
continue
}
binPath = filepath.Join(insidersDir, "server", "bin", "code-server-insiders")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
o.log.Infof("Execute %s: %v", binPath, command.WrapCommandError(out, err))
o.log.Info("Wait until vscode-server-insiders is installed...")
time.Sleep(time.Second * 3)
continue
}
break
}
return binPath
}
return binPath
}
func (o *VsCodeServer) findCodeServerBinary(location string) (string, error) {
serversDir := filepath.Join(location, "cli", "servers")
entries, err := os.ReadDir(serversDir)
if err != nil {
return "", fmt.Errorf("read dir %s: %w", serversDir, err)
} else if len(entries) == 0 {
return "", fmt.Errorf("read dir %s: install dir is missing", serversDir)
}
stableDir := ""
// find first entry with `Stable-` prefix
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if !strings.HasPrefix(entry.Name(), "Stable-") {
continue
}
stableDir = filepath.Join(serversDir, entry.Name())
}
if stableDir == "" {
return "", fmt.Errorf("read dir %s: install dir is missing", serversDir)
}
binPath := filepath.Join(stableDir, "server", "bin", "code-server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
out, err := exec.CommandContext(ctx, binPath, "--help").CombinedOutput()
cancel()
if err != nil {
return "", fmt.Errorf("execute %s: %w", binPath, command.WrapCommandError(out, err))
}
return binPath, nil
}
func prepareServerLocation(userName string, create bool, flavor Flavor) (string, error) {
var err error
homeFolder := ""
if userName != "" {
homeFolder, err = command.GetHome(userName)
} else {
homeFolder, err = util.UserHomeDir()
}
if err != nil {
return "", err
}
folderName := ".vscode-server"
switch flavor {
case FlavorStable:
folderName = ".vscode-server"
case FlavorInsiders:
folderName = ".vscode-server-insiders"
case FlavorCursor:
folderName = ".cursor-server"
case FlavorPositron:
folderName = ".positron-server"
case FlavorCodium:
folderName = ".vscodium-server"
}
folder := filepath.Join(homeFolder, folderName)
if create {
err = os.MkdirAll(folder, 0755)
if err != nil {
return "", err
}
}
return folder, nil
}