Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: deduplicate common code into sshutil #3129

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading