forked from TrueCloudLab/rclone
Fix parsing of remotes in moveto and copyto - fixes #1079
This commit is contained in:
parent
07dc76eff0
commit
30e97ad9ec
2 changed files with 26 additions and 22 deletions
29
cmd/cmd.go
29
cmd/cmd.go
|
@ -166,7 +166,11 @@ func NewFsSrcDst(args []string) (fs.Fs, fs.Fs) {
|
||||||
|
|
||||||
// RemoteSplit splits a remote into a parent and a leaf
|
// RemoteSplit splits a remote into a parent and a leaf
|
||||||
//
|
//
|
||||||
// if it returns parent as an empty string then it wasn't possible
|
// if it returns leaf as an empty string then remote is a directory
|
||||||
|
//
|
||||||
|
// if it returns parent as an empty string then that means the current directory
|
||||||
|
//
|
||||||
|
// The returned values have the property that parent + leaf == remote
|
||||||
func RemoteSplit(remote string) (parent string, leaf string) {
|
func RemoteSplit(remote string) (parent string, leaf string) {
|
||||||
// Split remote on :
|
// Split remote on :
|
||||||
i := strings.Index(remote, ":")
|
i := strings.Index(remote, ":")
|
||||||
|
@ -175,23 +179,13 @@ func RemoteSplit(remote string) (parent string, leaf string) {
|
||||||
if i >= 0 {
|
if i >= 0 {
|
||||||
remoteName = remote[:i+1]
|
remoteName = remote[:i+1]
|
||||||
remotePath = remote[i+1:]
|
remotePath = remote[i+1:]
|
||||||
}
|
} else if strings.HasSuffix(remotePath, "/") {
|
||||||
if remotePath == "" {
|
// if no : and ends with / must be directory
|
||||||
return "", ""
|
return remotePath, ""
|
||||||
}
|
}
|
||||||
// Construct new remote name without last segment
|
// Construct new remote name without last segment
|
||||||
parent, leaf = path.Split(remotePath)
|
parent, leaf = path.Split(remotePath)
|
||||||
if leaf == "" {
|
return remoteName + parent, leaf
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
if parent != "/" {
|
|
||||||
parent = strings.TrimSuffix(parent, "/")
|
|
||||||
}
|
|
||||||
parent = remoteName + parent
|
|
||||||
if parent == "" {
|
|
||||||
parent = "."
|
|
||||||
}
|
|
||||||
return parent, leaf
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFsSrcDstFiles creates a new src and dst fs from the arguments
|
// NewFsSrcDstFiles creates a new src and dst fs from the arguments
|
||||||
|
@ -203,7 +197,10 @@ func NewFsSrcDstFiles(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs
|
||||||
if srcFileName != "" {
|
if srcFileName != "" {
|
||||||
dstRemote, dstFileName = RemoteSplit(dstRemote)
|
dstRemote, dstFileName = RemoteSplit(dstRemote)
|
||||||
if dstRemote == "" {
|
if dstRemote == "" {
|
||||||
log.Fatalf("Can't find parent directory for %q", args[1])
|
dstRemote = "."
|
||||||
|
}
|
||||||
|
if dstFileName == "" {
|
||||||
|
log.Fatalf("%q is a directory", args[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fdst = newFsDst(dstRemote)
|
fdst = newFsDst(dstRemote)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -12,17 +13,23 @@ func TestRemoteSplit(t *testing.T) {
|
||||||
remote, wantParent, wantLeaf string
|
remote, wantParent, wantLeaf string
|
||||||
}{
|
}{
|
||||||
{"", "", ""},
|
{"", "", ""},
|
||||||
{"remote:", "", ""},
|
{"remote:", "remote:", ""},
|
||||||
{"remote:potato", "remote:", "potato"},
|
{"remote:potato", "remote:", "potato"},
|
||||||
{"remote:potato/sausage", "remote:potato", "sausage"},
|
{"remote:/", "remote:/", ""},
|
||||||
{"/", "", ""},
|
{"remote:/potato", "remote:/", "potato"},
|
||||||
|
{"remote:/potato/potato", "remote:/potato/", "potato"},
|
||||||
|
{"remote:potato/sausage", "remote:potato/", "sausage"},
|
||||||
|
{"/", "/", ""},
|
||||||
{"/root", "/", "root"},
|
{"/root", "/", "root"},
|
||||||
{"/a/b", "/a", "b"},
|
{"/a/b", "/a/", "b"},
|
||||||
{"root", ".", "root"},
|
{"root", "", "root"},
|
||||||
{"a/b", "a", "b"},
|
{"a/b", "a/", "b"},
|
||||||
|
{"root/", "root/", ""},
|
||||||
|
{"a/b/", "a/b/", ""},
|
||||||
} {
|
} {
|
||||||
gotParent, gotLeaf := RemoteSplit(test.remote)
|
gotParent, gotLeaf := RemoteSplit(test.remote)
|
||||||
assert.Equal(t, test.wantParent, gotParent, test.remote)
|
assert.Equal(t, test.wantParent, gotParent, test.remote)
|
||||||
assert.Equal(t, test.wantLeaf, gotLeaf, test.remote)
|
assert.Equal(t, test.wantLeaf, gotLeaf, test.remote)
|
||||||
|
assert.Equal(t, test.remote, gotParent+gotLeaf, fmt.Sprintf("%s: %q + %q != %q", test.remote, gotParent, gotLeaf, test.remote))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue