Skip to content

Commit

Permalink
Merge pull request #451 from fawick/master
Browse files Browse the repository at this point in the history
Support custom SSH ports in URL of SFTP-repo
  • Loading branch information
fd0 committed Feb 16, 2016
2 parents d918d0f + 2d94e71 commit 273d028
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backend/sftp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ var configTests = []struct {
"sftp://host//dir/subdir",
Config{Host: "host", Dir: "/dir/subdir"},
},
{
"sftp://host:10022//dir/subdir",
Config{Host: "host:10022", Dir: "/dir/subdir"},
},
{
"sftp://user@host:10022//dir/subdir",
Config{User: "user", Host: "host:10022", Dir: "/dir/subdir"},
},

// second form, user specified sftp:user@host:/dir
{
Expand Down
6 changes: 5 additions & 1 deletion backend/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
}

func buildSSHCommand(cfg Config) []string {
args := []string{cfg.Host}
hostport := strings.Split(cfg.Host, ":")
args := []string{hostport[0]}
if len(hostport) > 1 {
args = append(args, "-p", hostport[1])
}
if cfg.User != "" {
args = append(args, "-l")
args = append(args, cfg.User)
Expand Down
46 changes: 46 additions & 0 deletions backend/sftp/sshcmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package sftp

import "testing"

var sshcmdTests = []struct {
cfg Config
s []string
}{
{
Config{User: "user", Host: "host", Dir: "dir/subdir"},
[]string{"host", "-l", "user", "-s", "sftp"},
},
{
Config{Host: "host", Dir: "dir/subdir"},
[]string{"host", "-s", "sftp"},
},
{
Config{Host: "host:10022", Dir: "/dir/subdir"},
[]string{"host", "-p", "10022", "-s", "sftp"},
},
{
Config{User: "user", Host: "host:10022", Dir: "/dir/subdir"},
[]string{"host", "-p", "10022", "-l", "user", "-s", "sftp"},
},
}

func TestBuildSSHCommand(t *testing.T) {
for i, test := range sshcmdTests {
cmd := buildSSHCommand(test.cfg)
failed := false
if len(cmd) != len(test.s) {
failed = true
} else {
for l := range test.s {
if test.s[l] != cmd[l] {
failed = true
break
}
}
}
if failed {
t.Errorf("test %d: wrong cmd, want:\n %v\ngot:\n %v",
i, test.s, cmd)
}
}
}

0 comments on commit 273d028

Please sign in to comment.