Skip to content

Commit

Permalink
Refactor: deduplicate common code into sshutil
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Jan 19, 2025
1 parent 9b8d2a7 commit 36e61b8
Show file tree
Hide file tree
Showing 3 changed files with 41 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.GetSSHArguments()
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.GetSSHArguments()
if err != nil {
return err
}

sshOpts, err := sshutil.SSHOpts(
Expand Down
34 changes: 34 additions & 0 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,44 @@ 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 GetSSHArguments() (string, []string, error) {

Check failure on line 33 in pkg/sshutil/sshutil.go

View workflow job for this annotation

GitHub Actions / Lints

unnamedResult: consider giving a name to these results (gocritic)
var arg0 string
var arg0Args []string
var 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 36e61b8

Please sign in to comment.