1
0
mirror of https://github.com/restic/restic.git synced 2024-06-26 07:49:01 +02:00

Support custom SSH ports in URL of SFTP-repo

This commit is contained in:
Fabian Wickborn 2016-02-15 19:17:41 +01:00
parent d918d0f0c3
commit 2d94e71117
3 changed files with 59 additions and 1 deletions

View File

@ -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
{

View File

@ -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)

View File

@ -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)
}
}
}