mirror of
https://github.com/restic/restic.git
synced 2025-03-13 16:20:52 +00:00
Merge pull request #451 from fawick/master
Support custom SSH ports in URL of SFTP-repo
This commit is contained in:
commit
273d028a82
@ -19,6 +19,14 @@ var configTests = []struct {
|
|||||||
"sftp://host//dir/subdir",
|
"sftp://host//dir/subdir",
|
||||||
Config{Host: "host", Dir: "/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
|
// second form, user specified sftp:user@host:/dir
|
||||||
{
|
{
|
||||||
|
@ -97,7 +97,11 @@ func Open(dir string, program string, args ...string) (*SFTP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func buildSSHCommand(cfg Config) []string {
|
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 != "" {
|
if cfg.User != "" {
|
||||||
args = append(args, "-l")
|
args = append(args, "-l")
|
||||||
args = append(args, cfg.User)
|
args = append(args, cfg.User)
|
||||||
|
46
backend/sftp/sshcmd_test.go
Normal file
46
backend/sftp/sshcmd_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user