Skip to content

Commit

Permalink
fix: allow passing multiple args to server ssh command (#729)
Browse files Browse the repository at this point in the history
Related to #711

Closes #728
  • Loading branch information
jooola committed Apr 17, 2024
1 parent 73579dd commit d98aee5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
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"
}

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

0 comments on commit d98aee5

Please sign in to comment.