From 2d94e711172cc33be4c741553d1560cff1709f70 Mon Sep 17 00:00:00 2001 From: Fabian Wickborn Date: Mon, 15 Feb 2016 19:17:41 +0100 Subject: [PATCH] Support custom SSH ports in URL of SFTP-repo --- backend/sftp/config_test.go | 8 +++++++ backend/sftp/sftp.go | 6 ++++- backend/sftp/sshcmd_test.go | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 backend/sftp/sshcmd_test.go diff --git a/backend/sftp/config_test.go b/backend/sftp/config_test.go index 7d5399de4..63a46ae3b 100644 --- a/backend/sftp/config_test.go +++ b/backend/sftp/config_test.go @@ -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 { diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index a4b3ac73a..ddd35a56f 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -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) diff --git a/backend/sftp/sshcmd_test.go b/backend/sftp/sshcmd_test.go new file mode 100644 index 000000000..d98309c67 --- /dev/null +++ b/backend/sftp/sshcmd_test.go @@ -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) + } + } +}