Skip to content

Commit a0e38e6

Browse files
committed
refactor: centralize SSH executable creation
Signed-off-by: Bartek Mucha <muchzill4@gmail.com>
1 parent d7a4283 commit a0e38e6

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

cmd/limactl/copy.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ func copyAction(cmd *cobra.Command, args []string) error {
8484
scpFlags = append(scpFlags, "-r")
8585
}
8686
// this assumes that ssh and scp come from the same place, but scp has no -V
87-
legacySSH := sshutil.DetectOpenSSHVersion(sshutil.SSHExe{Executable: "ssh"}).LessThan(*semver.New("8.0.0"))
87+
sshExe, err := sshutil.NewSSHExe()
88+
if err != nil {
89+
return err
90+
}
91+
legacySSH := sshutil.DetectOpenSSHVersion(sshExe).LessThan(*semver.New("8.0.0"))
8892
for _, arg := range args {
8993
if runtime.GOOS == "windows" {
9094
if filepath.IsAbs(arg) {
@@ -135,14 +139,22 @@ func copyAction(cmd *cobra.Command, args []string) error {
135139
// arguments such as ControlPath. This is preferred as we can multiplex
136140
// sessions without re-authenticating (MaxSessions permitting).
137141
for _, inst := range instances {
138-
sshOpts, err = sshutil.SSHOpts(sshutil.SSHExe{Executable: "ssh"}, inst.Dir, *inst.Config.User.Name, false, false, false, false)
142+
sshExe, err := sshutil.NewSSHExe()
143+
if err != nil {
144+
return err
145+
}
146+
sshOpts, err = sshutil.SSHOpts(sshExe, inst.Dir, *inst.Config.User.Name, false, false, false, false)
139147
if err != nil {
140148
return err
141149
}
142150
}
143151
} else {
144152
// Copying among multiple hosts; we can't pass in host-specific options.
145-
sshOpts, err = sshutil.CommonOpts(sshutil.SSHExe{Executable: "ssh"}, false)
153+
sshExe, err := sshutil.NewSSHExe()
154+
if err != nil {
155+
return err
156+
}
157+
sshOpts, err = sshutil.CommonOpts(sshExe, false)
146158
if err != nil {
147159
return err
148160
}

cmd/limactl/shell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
196196
)
197197
}
198198

199-
sshExe, err := sshutil.SSHArguments()
199+
sshExe, err := sshutil.NewSSHExe()
200200
if err != nil {
201201
return err
202202
}
@@ -236,7 +236,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
236236
script,
237237
}...)
238238
allArgs := append(sshExe.Args, sshArgs...)
239-
sshCmd := exec.Command(sshExe.Executable, allArgs...)
239+
sshCmd := exec.Command(sshExe.Exe, allArgs...)
240240
sshCmd.Stdin = os.Stdin
241241
sshCmd.Stdout = os.Stdout
242242
sshCmd.Stderr = os.Stderr

cmd/limactl/show-ssh.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
9292
}
9393
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s %s`.",
9494
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
95+
sshExe, err := sshutil.NewSSHExe()
96+
if err != nil {
97+
return err
98+
}
9599
opts, err := sshutil.SSHOpts(
96-
sshutil.SSHExe{Executable: "ssh"},
100+
sshExe,
97101
inst.Dir,
98102
*inst.Config.User.Name,
99103
*inst.Config.SSH.LoadDotSSHPubKeys,

cmd/limactl/tunnel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
8282
}
8383
}
8484

85-
sshExe, err := sshutil.SSHArguments()
85+
sshExe, err := sshutil.NewSSHExe()
8686
if err != nil {
8787
return err
8888
}
@@ -108,7 +108,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
108108
inst.SSHAddress,
109109
}...)
110110
allArgs := append(sshExe.Args, sshArgs...)
111-
sshCmd := exec.Command(sshExe.Executable, allArgs...)
111+
sshCmd := exec.Command(sshExe.Exe, allArgs...)
112112
sshCmd.Stdout = stderr
113113
sshCmd.Stderr = stderr
114114
logrus.Debugf("executing ssh (may take a long)): %+v", sshCmd.Args)

pkg/hostagent/hostagent.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
146146
return nil, err
147147
}
148148

149+
sshExe, err := sshutil.NewSSHExe()
150+
if err != nil {
151+
return nil, err
152+
}
149153
sshOpts, err := sshutil.SSHOpts(
150-
sshutil.SSHExe{Executable: "ssh"},
154+
sshExe,
151155
inst.Dir,
152156
*inst.Config.User.Name,
153157
*inst.Config.SSH.LoadDotSSHPubKeys,

pkg/sshutil/sshutil.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import (
3636
const EnvShellSSH = "SSH"
3737

3838
type SSHExe struct {
39-
Executable string
40-
Args []string
39+
Exe string
40+
Args []string
4141
}
4242

43-
func SSHArguments() (SSHExe, error) {
43+
func NewSSHExe() (SSHExe, error) {
4444
var sshExe SSHExe
4545

4646
if sshShell := os.Getenv(EnvShellSSH); sshShell != "" {
@@ -50,7 +50,7 @@ func SSHArguments() (SSHExe, error) {
5050
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
5151
"Falling back to 'ssh' command", EnvShellSSH)
5252
case len(sshShellFields) > 0:
53-
sshExe.Executable = sshShellFields[0]
53+
sshExe.Exe = sshShellFields[0]
5454
if len(sshShellFields) > 1 {
5555
sshExe.Args = sshShellFields[1:]
5656
}
@@ -62,7 +62,7 @@ func SSHArguments() (SSHExe, error) {
6262
if err != nil {
6363
return SSHExe{}, err
6464
}
65-
sshExe.Executable = executable
65+
sshExe.Exe = executable
6666

6767
return sshExe, nil
6868
}
@@ -376,7 +376,7 @@ func detectOpenSSHInfo(sshExe SSHExe) openSSHInfo {
376376
)
377377
// TODO: Fix this function to properly handle complex SSH commands like "kitten ssh"
378378
// The current LookPath, os.Stat, and caching logic doesn't work well for multi-word commands
379-
path, err := exec.LookPath(sshExe.Executable)
379+
path, err := exec.LookPath(sshExe.Exe)
380380
if err != nil {
381381
logrus.Warnf("failed to find ssh executable: %v", err)
382382
} else {
@@ -391,7 +391,7 @@ func detectOpenSSHInfo(sshExe SSHExe) openSSHInfo {
391391
}
392392
// -V should be last
393393
allArgs := append(sshExe.Args, "-o", "GSSAPIAuthentication=no", "-V")
394-
cmd := exec.Command(sshExe.Executable, allArgs...)
394+
cmd := exec.Command(sshExe.Exe, allArgs...)
395395
cmd.Stderr = &stderr
396396
if err := cmd.Run(); err != nil {
397397
logrus.Warnf("failed to run %v: stderr=%q", cmd.Args, stderr.String())

0 commit comments

Comments
 (0)