Skip to content

Commit c1e9237

Browse files
committed
feat(BREV-2866): open shell in expected container working directory
1 parent 6aece4f commit c1e9237

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

pkg/cmd/shell/shell.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID
120120
// legacy environments wont support this and cause errrors,
121121
// but we don't want to block the user from using the shell
122122
_ = writeconnectionevent.WriteWCEOnEnv(sstore, workspace.DNS)
123-
err = runSSH(sshName)
123+
err = runSSH(sshName, host)
124124
if err != nil {
125125
return breverrors.WrapAndTrace(err)
126126
}
@@ -144,9 +144,15 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID
144144
return nil
145145
}
146146

147-
func runSSH(sshAlias string) error {
147+
func runSSH(sshAlias string, host bool) error {
148148
sshAgentEval := "eval $(ssh-agent -s)"
149-
cmd := fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias)
149+
var cmd string
150+
if host {
151+
cmd = fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias)
152+
} else {
153+
// SSH into VM and respect container WORKDIR if containerized, otherwise use default directory
154+
cmd = fmt.Sprintf("%s && ssh -t %s 'DIR=$(readlink -f /proc/1/cwd 2>/dev/null || pwd); cd \"$DIR\" || echo \"Warning: Could not access container directory\" >&2; exec ${SHELL:-/bin/sh}'", sshAgentEval, sshAlias)
155+
}
150156

151157
sshCmd := exec.Command("bash", "-c", cmd) //nolint:gosec //cmd is user input
152158
sshCmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)