Skip to content

Commit 36e61b8

Browse files
committed
Refactor: deduplicate common code into sshutil
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 9b8d2a7 commit 36e61b8

File tree

3 files changed

+41
-53
lines changed

3 files changed

+41
-53
lines changed

cmd/limactl/shell.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@ import (
1313
"github.com/lima-vm/lima/pkg/sshutil"
1414
"github.com/lima-vm/lima/pkg/store"
1515
"github.com/mattn/go-isatty"
16-
"github.com/mattn/go-shellwords"
1716
"github.com/sirupsen/logrus"
1817
"github.com/spf13/cobra"
1918
)
2019

21-
// Environment variable that allows configuring the command (alias) to execute
22-
// in place of the 'ssh' executable.
23-
const envShellSSH = "SSH"
24-
2520
const shellHelp = `Execute shell in Lima
2621
2722
lima command is provided as an alias for limactl shell $LIMA_INSTANCE. $LIMA_INSTANCE defaults to "` + DefaultInstanceName + `".
2823
2924
By default, the first 'ssh' executable found in the host's PATH is used to connect to the Lima instance.
30-
A custom ssh alias can be used instead by setting the $` + envShellSSH + ` environment variable.
25+
A custom ssh alias can be used instead by setting the $` + sshutil.EnvShellSSH + ` environment variable.
3126
3227
Hint: try --debug to show the detailed logs, if it seems hanging (mostly due to some SSH issue).
3328
`
@@ -142,28 +137,9 @@ func shellAction(cmd *cobra.Command, args []string) error {
142137
)
143138
}
144139

145-
var arg0 string
146-
var arg0Args []string
147-
148-
if sshShell := os.Getenv(envShellSSH); sshShell != "" {
149-
sshShellFields, err := shellwords.Parse(sshShell)
150-
switch {
151-
case err != nil:
152-
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
153-
"Falling back to 'ssh' command", envShellSSH)
154-
case len(sshShellFields) > 0:
155-
arg0 = sshShellFields[0]
156-
if len(sshShellFields) > 1 {
157-
arg0Args = sshShellFields[1:]
158-
}
159-
}
160-
}
161-
162-
if arg0 == "" {
163-
arg0, err = exec.LookPath("ssh")
164-
if err != nil {
165-
return err
166-
}
140+
arg0, arg0Args, err := sshutil.GetSSHArguments()
141+
if err != nil {
142+
return err
167143
}
168144

169145
sshOpts, err := sshutil.SSHOpts(

cmd/limactl/tunnel.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/lima-vm/lima/pkg/freeport"
1212
"github.com/lima-vm/lima/pkg/sshutil"
1313
"github.com/lima-vm/lima/pkg/store"
14-
"github.com/mattn/go-shellwords"
1514
"github.com/sirupsen/logrus"
1615
"github.com/spf13/cobra"
1716
)
@@ -79,30 +78,9 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
7978
}
8079
}
8180

82-
var (
83-
arg0 string
84-
arg0Args []string
85-
)
86-
// FIXME: deduplicate the code clone across `limactl shell` and `limactl tunnel`
87-
if sshShell := os.Getenv(envShellSSH); sshShell != "" {
88-
sshShellFields, err := shellwords.Parse(sshShell)
89-
switch {
90-
case err != nil:
91-
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
92-
"Falling back to 'ssh' command", envShellSSH)
93-
case len(sshShellFields) > 0:
94-
arg0 = sshShellFields[0]
95-
if len(sshShellFields) > 1 {
96-
arg0Args = sshShellFields[1:]
97-
}
98-
}
99-
}
100-
101-
if arg0 == "" {
102-
arg0, err = exec.LookPath("ssh")
103-
if err != nil {
104-
return err
105-
}
81+
arg0, arg0Args, err := sshutil.GetSSHArguments()
82+
if err != nil {
83+
return err
10684
}
10785

10886
sshOpts, err := sshutil.SSHOpts(

pkg/sshutil/sshutil.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,44 @@ import (
2121
"github.com/lima-vm/lima/pkg/osutil"
2222
"github.com/lima-vm/lima/pkg/store/dirnames"
2323
"github.com/lima-vm/lima/pkg/store/filenames"
24+
"github.com/mattn/go-shellwords"
2425
"github.com/sirupsen/logrus"
2526
"golang.org/x/sys/cpu"
2627
)
2728

29+
// Environment variable that allows configuring the command (alias) to execute
30+
// in place of the 'ssh' executable.
31+
const EnvShellSSH = "SSH"
32+
33+
func GetSSHArguments() (string, []string, error) {
34+
var arg0 string
35+
var arg0Args []string
36+
var err error
37+
38+
if sshShell := os.Getenv(EnvShellSSH); sshShell != "" {
39+
sshShellFields, err := shellwords.Parse(sshShell)
40+
switch {
41+
case err != nil:
42+
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
43+
"Falling back to 'ssh' command", EnvShellSSH)
44+
case len(sshShellFields) > 0:
45+
arg0 = sshShellFields[0]
46+
if len(sshShellFields) > 1 {
47+
arg0Args = sshShellFields[1:]
48+
}
49+
}
50+
}
51+
52+
if arg0 == "" {
53+
arg0, err = exec.LookPath("ssh")
54+
if err != nil {
55+
return "", []string{""}, err
56+
}
57+
}
58+
59+
return arg0, arg0Args, nil
60+
}
61+
2862
type PubKey struct {
2963
Filename string
3064
Content string

0 commit comments

Comments
 (0)