Skip to content

Commit 86baa12

Browse files
feat(BREV-2866): open shell in expected container working directory (#308)
* feat(BREV-2866): open shell in expected container working directory * feat(BREV-2866): preserve login shell environment
1 parent 1a12fd5 commit 86baa12

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
@@ -126,7 +126,7 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID
126126
// legacy environments wont support this and cause errrors,
127127
// but we don't want to block the user from using the shell
128128
_ = writeconnectionevent.WriteWCEOnEnv(sstore, workspace.DNS)
129-
err = runSSH(sshName)
129+
err = runSSH(sshName, host)
130130
if err != nil {
131131
return breverrors.WrapAndTrace(err)
132132
}
@@ -193,9 +193,15 @@ func runSSHWithPort(target string, port int32, identityFile string) error {
193193
return nil
194194
}
195195

196-
func runSSH(sshAlias string) error {
196+
func runSSH(sshAlias string, host bool) error {
197197
sshAgentEval := "eval $(ssh-agent -s)"
198-
cmd := fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias)
198+
var cmd string
199+
if host {
200+
cmd = fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias)
201+
} else {
202+
// SSH into VM and respect container WORKDIR if containerized, otherwise use default directory
203+
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 -l ${SHELL:-/bin/sh}'", sshAgentEval, sshAlias)
204+
}
199205

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

0 commit comments

Comments
 (0)