2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-24 11:55:28 +00:00

Merge pull request #451 from fawick/master

Support custom SSH ports in URL of SFTP-repo
This commit is contained in:
Alexander Neumann 2016-02-16 21:13:42 +01:00
commit 273d028a82
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)
}
}
}