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

fix: allow passing multiple args to server ssh command #729

Merged
merged 2 commits into from
Apr 17, 2024
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
6 changes: 4 additions & 2 deletions internal/cmd/server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
)

var SSHCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "ssh [options] <server> [command]",
Use: "ssh [options] <server> [command...]",
Short: "Spawn an SSH connection for the server",
Args: util.ValidateLenient,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Server().Names)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
Expand Down Expand Up @@ -55,7 +57,7 @@ var SSHCmd = base.Cmd{
}

sshArgs := []string{"-l", user, "-p", strconv.Itoa(port), ipAddress.String()}
sshCommand := exec.Command("ssh", append(sshArgs, args[1:]...)...)
sshCommand := exec.Command(s.Config().SSHPath(), append(sshArgs, args[1:]...)...)
sshCommand.Stdin = os.Stdin
sshCommand.Stdout = os.Stdout
sshCommand.Stderr = os.Stderr
Expand Down
47 changes: 47 additions & 0 deletions internal/cmd/server/ssh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package server_test

import (
"net"
"testing"

"github.com/golang/mock/gomock"

"github.com/hetznercloud/cli/internal/cmd/server"
"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestSSH(t *testing.T) {
srv := hcloud.Server{
ID: 42,
Name: "server1",
Status: hcloud.ServerStatusRunning,
PublicNet: hcloud.ServerPublicNet{
IPv4: hcloud.ServerPublicNetIPv4{
IP: net.ParseIP("192.168.0.2"),
},
},
}

preRun := func(t *testing.T, fx *testutil.Fixture) {
fx.Client.ServerClient.EXPECT().
Get(gomock.Any(), srv.Name).
Return(&srv, nil, nil)

fx.Config.EXPECT().SSHPath().Return("echo")
}

testutil.TestCommand(t, &server.SSHCmd, map[string]testutil.TestCase{
"single arg": {
Args: []string{"ssh", srv.Name},
PreRun: preRun,
ExpOut: "-l root -p 22 192.168.0.2\n",
},
"many args": {
Args: []string{"ssh", srv.Name, "ls", "-al"},
PreRun: preRun,
ExpOut: "-l root -p 22 192.168.0.2 ls -al\n",
},
})

}
6 changes: 6 additions & 0 deletions internal/state/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Config interface {
SetContexts([]*Context)
Endpoint() string
SetEndpoint(string)

SSHPath() string
}

type Context struct {
Expand Down Expand Up @@ -94,6 +96,10 @@ func (cfg *config) SetEndpoint(endpoint string) {
cfg.endpoint = endpoint
}

func (cfg *config) SSHPath() string {
return "ssh"
}
jooola marked this conversation as resolved.
Show resolved Hide resolved

func ContextNames(cfg Config) []string {
ctxs := cfg.Contexts()
names := make([]string, len(ctxs))
Expand Down
14 changes: 14 additions & 0 deletions internal/state/config/zz_config_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/testutil/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TestableCommand interface {

type TestCase struct {
Args []string
PreRun func(t *testing.T, fx *Fixture)
ExpOut string
ExpOutType DataType
ExpErrOut string
Expand Down Expand Up @@ -65,6 +66,10 @@ func TestCommand(t *testing.T, cmd TestableCommand, cases map[string]TestCase) {

rootCmd.AddCommand(cmd.CobraCommand(fx.State()))

if testCase.PreRun != nil {
testCase.PreRun(t, fx)
}

out, errOut, err := fx.Run(rootCmd, testCase.Args)

assert.NoError(t, err)
Expand Down