Skip to content

Commit

Permalink
Merge pull request #3129 from afbjorklund/ssh-arguments
Browse files Browse the repository at this point in the history
Refactor: deduplicate common code into sshutil
  • Loading branch information
AkihiroSuda authored Jan 21, 2025
2 parents c9bedcc + f9f6711 commit 3a59746
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 53 deletions.
32 changes: 4 additions & 28 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ import (
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/mattn/go-isatty"
"github.com/mattn/go-shellwords"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// Environment variable that allows configuring the command (alias) to execute
// in place of the 'ssh' executable.
const envShellSSH = "SSH"

const shellHelp = `Execute shell in Lima
lima command is provided as an alias for limactl shell $LIMA_INSTANCE. $LIMA_INSTANCE defaults to "` + DefaultInstanceName + `".
By default, the first 'ssh' executable found in the host's PATH is used to connect to the Lima instance.
A custom ssh alias can be used instead by setting the $` + envShellSSH + ` environment variable.
A custom ssh alias can be used instead by setting the $` + sshutil.EnvShellSSH + ` environment variable.
Hint: try --debug to show the detailed logs, if it seems hanging (mostly due to some SSH issue).
`
Expand Down Expand Up @@ -142,28 +137,9 @@ func shellAction(cmd *cobra.Command, args []string) error {
)
}

var arg0 string
var arg0Args []string

if sshShell := os.Getenv(envShellSSH); sshShell != "" {
sshShellFields, err := shellwords.Parse(sshShell)
switch {
case err != nil:
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
"Falling back to 'ssh' command", envShellSSH)
case len(sshShellFields) > 0:
arg0 = sshShellFields[0]
if len(sshShellFields) > 1 {
arg0Args = sshShellFields[1:]
}
}
}

if arg0 == "" {
arg0, err = exec.LookPath("ssh")
if err != nil {
return err
}
arg0, arg0Args, err := sshutil.SSHArguments()
if err != nil {
return err
}

sshOpts, err := sshutil.SSHOpts(
Expand Down
28 changes: 3 additions & 25 deletions cmd/limactl/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/lima-vm/lima/pkg/freeport"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/mattn/go-shellwords"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -79,30 +78,9 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
}
}

var (
arg0 string
arg0Args []string
)
// FIXME: deduplicate the code clone across `limactl shell` and `limactl tunnel`
if sshShell := os.Getenv(envShellSSH); sshShell != "" {
sshShellFields, err := shellwords.Parse(sshShell)
switch {
case err != nil:
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
"Falling back to 'ssh' command", envShellSSH)
case len(sshShellFields) > 0:
arg0 = sshShellFields[0]
if len(sshShellFields) > 1 {
arg0Args = sshShellFields[1:]
}
}
}

if arg0 == "" {
arg0, err = exec.LookPath("ssh")
if err != nil {
return err
}
arg0, arg0Args, err := sshutil.SSHArguments()
if err != nil {
return err
}

sshOpts, err := sshutil.SSHOpts(
Expand Down
30 changes: 30 additions & 0 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,40 @@ import (
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/mattn/go-shellwords"
"github.com/sirupsen/logrus"
"golang.org/x/sys/cpu"
)

// Environment variable that allows configuring the command (alias) to execute
// in place of the 'ssh' executable.
const EnvShellSSH = "SSH"

func SSHArguments() (arg0 string, arg0Args []string, err error) {
if sshShell := os.Getenv(EnvShellSSH); sshShell != "" {
sshShellFields, err := shellwords.Parse(sshShell)
switch {
case err != nil:
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+
"Falling back to 'ssh' command", EnvShellSSH)
case len(sshShellFields) > 0:
arg0 = sshShellFields[0]
if len(sshShellFields) > 1 {
arg0Args = sshShellFields[1:]
}
}
}

if arg0 == "" {
arg0, err = exec.LookPath("ssh")
if err != nil {
return "", []string{""}, err
}
}

return arg0, arg0Args, nil
}

type PubKey struct {
Filename string
Content string
Expand Down

0 comments on commit 3a59746

Please sign in to comment.