Skip to content

Commit 648c47d

Browse files
committed
feat(shell): allow configuring ssh alias
If the LIMASHELL_SSH environment variable is set, 'limactl shell' will consider this as an alias for the 'ssh' command to execute, instead of looking up a 'ssh' executable in the PATH. The provided command can be anything that is CLI-compatible with the OpenSSH client, such as a shell script that passes all provided arguments to 'ssh'. Signed-off-by: Antoine Cotten <[email protected]>
1 parent 1599b8c commit 648c47d

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

cmd/limactl/shell.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ import (
1212
"github.com/lima-vm/lima/pkg/sshutil"
1313
"github.com/lima-vm/lima/pkg/store"
1414
"github.com/mattn/go-isatty"
15+
"github.com/mattn/go-shellwords"
1516
"github.com/sirupsen/logrus"
1617
"github.com/spf13/cobra"
1718
)
1819

20+
// Environment variable that allows configuring the command (alias) to execute
21+
// in place of the 'ssh' executable.
22+
const envShellSSH = "SSH"
23+
1924
var shellHelp = `Execute shell in Lima
2025
2126
lima command is provided as an alias for limactl shell $LIMA_INSTANCE. $LIMA_INSTANCE defaults to "` + DefaultInstanceName + `".
2227
28+
By default, the first 'ssh' executable found in the host's PATH is used to connect to the Lima instance.
29+
A custom ssh alias can be used instead by setting the $` + envShellSSH + ` environment variable.
30+
2331
Hint: try --debug to show the detailed logs, if it seems hanging (mostly due to some SSH issue).
2432
`
2533

@@ -126,9 +134,28 @@ func shellAction(cmd *cobra.Command, args []string) error {
126134
)
127135
}
128136

129-
arg0, err := exec.LookPath("ssh")
130-
if err != nil {
131-
return err
137+
var arg0 string
138+
var arg0Args []string
139+
140+
if sshShell := os.Getenv(envShellSSH); sshShell != "" {
141+
sshShellFields, err := shellwords.Parse(sshShell)
142+
switch {
143+
case err != nil:
144+
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
145+
"Falling back to 'ssh' command", envShellSSH)
146+
case len(sshShellFields) > 0:
147+
arg0 = sshShellFields[0]
148+
if len(sshShellFields) > 1 {
149+
arg0Args = sshShellFields[1:]
150+
}
151+
}
152+
}
153+
154+
if arg0 == "" {
155+
arg0, err = exec.LookPath("ssh")
156+
if err != nil {
157+
return err
158+
}
132159
}
133160

134161
sshOpts, err := sshutil.SSHOpts(inst.Dir, *y.SSH.LoadDotSSHPubKeys, *y.SSH.ForwardAgent, *y.SSH.ForwardX11, *y.SSH.ForwardX11Trusted)
@@ -151,7 +178,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
151178
"--",
152179
script,
153180
}...)
154-
sshCmd := exec.Command(arg0, sshArgs...)
181+
sshCmd := exec.Command(arg0, append(arg0Args, sshArgs...)...)
155182
sshCmd.Stdin = os.Stdin
156183
sshCmd.Stdout = os.Stdout
157184
sshCmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)