Skip to content

Commit 3042f7b

Browse files
authored
Merge pull request #254 from brevdev/fix-workspace
dynamically set workspace home dir based on SSHUser
2 parents e4af994 + ca6311d commit 3042f7b

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

pkg/cmd/open/open.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ func runOpenCommand(t *terminal.Terminal, tstore OpenStore, wsIDOrName string, s
179179
return breverrors.WorkspaceNotRunning{Status: workspace.Status}
180180
}
181181

182-
projPath := workspace.GetProjectFolderPath()
182+
projPath, err := workspace.GetProjectFolderPath()
183+
if err != nil {
184+
return breverrors.WrapAndTrace(err)
185+
}
183186
if directory != "" {
184187
projPath = directory
185188
}

pkg/entity/entity.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,20 @@ func MapContainsKey[K comparable, V any](m map[K]V, key K) bool {
444444
return ok
445445
}
446446

447-
func (w Workspace) GetProjectFolderPath() string {
447+
func (w Workspace) GetProjectFolderPath() (string, error) {
448448
var prefix string
449449
if MapContainsKey(LegacyWorkspaceGroups, w.WorkspaceGroupID) {
450450
prefix = "/home/brev/workspace"
451451
} else {
452-
prefix = "/home/ubuntu"
452+
if w.SSHUser == "" {
453+
return "", fmt.Errorf("workspace %s has empty SSH user (workspace may not be running)", w.ID)
454+
}
455+
prefix = "/home/" + w.SSHUser
453456
}
454457
var folderName string
455458
if w.IDEConfig.DefaultWorkingDir != "" { //nolint:gocritic // i like if else
456459
if path.IsAbs(w.IDEConfig.DefaultWorkingDir) {
457-
return w.IDEConfig.DefaultWorkingDir
460+
return w.IDEConfig.DefaultWorkingDir, nil
458461
} else {
459462
folderName = w.IDEConfig.DefaultWorkingDir
460463
}
@@ -471,13 +474,13 @@ func (w Workspace) GetProjectFolderPath() string {
471474
}
472475
}
473476
} else {
474-
return prefix
477+
return prefix, nil
475478
}
476479
} else {
477-
return prefix
480+
return prefix, nil
478481
}
479482

480-
return filepath.Join(prefix, folderName) // TODO make workspace dir configurable
483+
return filepath.Join(prefix, folderName), nil // TODO make workspace dir configurable
481484
}
482485

483486
func GetDefaultProjectFolderNameFromRepo(repo string) string {

pkg/ssh/sshconfigurer.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,16 @@ func makeSSHConfigEntryV2(workspace entity.Workspace, privateKeyPath string, clo
281281
privateKeyPath = "\"" + privateKeyPath + "\""
282282
if workspace.IsLegacy() {
283283
proxyCommand := makeProxyCommand(workspace.ID)
284+
projPath, err := workspace.GetProjectFolderPath()
285+
if err != nil {
286+
return "", breverrors.WrapAndTrace(err)
287+
}
284288
entry := SSHConfigEntryV2{
285289
Alias: alias,
286290
IdentityFile: privateKeyPath,
287291
User: "brev",
288292
ProxyCommand: proxyCommand,
289-
Dir: workspace.GetProjectFolderPath(),
293+
Dir: projPath,
290294
}
291295
tmpl, err := template.New(alias).Parse(SSHConfigEntryTemplateV2)
292296
if err != nil {
@@ -304,11 +308,15 @@ func makeSSHConfigEntryV2(workspace entity.Workspace, privateKeyPath string, clo
304308
hostname := workspace.GetHostname()
305309
if workspace.SSHProxyHostname == "" {
306310
port := workspace.GetSSHPort()
311+
projPath, err := workspace.GetProjectFolderPath()
312+
if err != nil {
313+
return "", breverrors.WrapAndTrace(err)
314+
}
307315
entry := SSHConfigEntryV2{
308316
Alias: alias,
309317
IdentityFile: privateKeyPath,
310318
User: user,
311-
Dir: workspace.GetProjectFolderPath(),
319+
Dir: projPath,
312320
HostName: hostname,
313321
Port: port,
314322
}
@@ -322,12 +330,16 @@ func makeSSHConfigEntryV2(workspace entity.Workspace, privateKeyPath string, clo
322330
}
323331
} else {
324332
proxyCommand := makeCloudflareSSHProxyCommand(cloudflaredBinaryPath, workspace.SSHProxyHostname)
333+
projPath, err := workspace.GetProjectFolderPath()
334+
if err != nil {
335+
return "", breverrors.WrapAndTrace(err)
336+
}
325337
entry := SSHConfigEntryV2{
326338
Alias: alias,
327339
IdentityFile: privateKeyPath,
328340
User: user,
329341
ProxyCommand: proxyCommand,
330-
Dir: workspace.GetProjectFolderPath(),
342+
Dir: projPath,
331343
}
332344
tmpl, err := template.New(alias).Parse(SSHConfigEntryTemplateV2)
333345
if err != nil {
@@ -344,11 +356,15 @@ func makeSSHConfigEntryV2(workspace entity.Workspace, privateKeyPath string, clo
344356
hostport := workspace.GetHostSSHPort()
345357
hostuser := workspace.GetHostSSHUser()
346358
if workspace.HostSSHProxyHostname == "" {
359+
projPath, err := workspace.GetProjectFolderPath()
360+
if err != nil {
361+
return "", breverrors.WrapAndTrace(err)
362+
}
347363
entry := SSHConfigEntryV2{
348364
Alias: alias,
349365
IdentityFile: privateKeyPath,
350366
User: hostuser,
351-
Dir: workspace.GetProjectFolderPath(),
367+
Dir: projPath,
352368
HostName: hostname,
353369
Port: hostport,
354370
}
@@ -362,12 +378,16 @@ func makeSSHConfigEntryV2(workspace entity.Workspace, privateKeyPath string, clo
362378
}
363379
} else {
364380
proxyCommand := makeCloudflareSSHProxyCommand(cloudflaredBinaryPath, workspace.HostSSHProxyHostname)
381+
projPath, err := workspace.GetProjectFolderPath()
382+
if err != nil {
383+
return "", breverrors.WrapAndTrace(err)
384+
}
365385
entry := SSHConfigEntryV2{
366386
Alias: alias,
367387
IdentityFile: privateKeyPath,
368388
User: hostuser,
369389
ProxyCommand: proxyCommand,
370-
Dir: workspace.GetProjectFolderPath(),
390+
Dir: projPath,
371391
}
372392
tmpl, err := template.New(alias).Parse(SSHConfigEntryTemplateV2)
373393
if err != nil {

0 commit comments

Comments
 (0)