Skip to content

Commit c9097fe

Browse files
committed
Add support for exiting ssh session
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 710e900 commit c9097fe

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmd/limactl/shell.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/lima-vm/lima/pkg/ioutilx"
1919
"github.com/lima-vm/lima/pkg/sshutil"
2020
"github.com/lima-vm/lima/pkg/store"
21+
"github.com/lima-vm/sshocker/pkg/ssh"
2122
"github.com/mattn/go-isatty"
2223
"github.com/sirupsen/logrus"
2324
"github.com/spf13/cobra"
@@ -49,6 +50,7 @@ func newShellCommand() *cobra.Command {
4950

5051
shellCmd.Flags().String("shell", "", "shell interpreter, e.g. /bin/bash")
5152
shellCmd.Flags().String("workdir", "", "working directory")
53+
shellCmd.Flags().Bool("reconnect", false, "reconnect to the SSH session")
5254
return shellCmd
5355
}
5456

@@ -81,6 +83,24 @@ func shellAction(cmd *cobra.Command, args []string) error {
8183
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
8284
}
8385

86+
restart, err := cmd.Flags().GetBool("restart-ssh")
87+
if err != nil {
88+
return err
89+
}
90+
if restart {
91+
logrus.Infof("Exiting ssh session for the instance %q", instName)
92+
93+
sshConfig := &ssh.SSHConfig{
94+
ConfigFile: inst.SSHConfigFile,
95+
Persist: false,
96+
AdditionalArgs: []string{},
97+
}
98+
99+
if err := ssh.ExitMaster(inst.Hostname, inst.SSHLocalPort, sshConfig); err != nil {
100+
return err
101+
}
102+
}
103+
84104
// When workDir is explicitly set, the shell MUST have workDir as the cwd, or exit with an error.
85105
//
86106
// changeDirCmd := "cd workDir || exit 1" if workDir != ""

0 commit comments

Comments
 (0)