overdue
This commit is contained in:
50
.rclone_repo/fs/fspath/path.go
Executable file
50
.rclone_repo/fs/fspath/path.go
Executable file
@@ -0,0 +1,50 @@
|
||||
// Package fspath contains routines for fspath manipulation
|
||||
package fspath
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/ncw/rclone/fs/driveletter"
|
||||
)
|
||||
|
||||
// Matcher is a pattern to match an rclone URL
|
||||
var Matcher = regexp.MustCompile(`^(:?[\w_ -]+):(.*)$`)
|
||||
|
||||
// Parse deconstructs a remote path into configName and fsPath
|
||||
//
|
||||
// If the path is a local path then configName will be returned as "".
|
||||
//
|
||||
// So "remote:path/to/dir" will return "remote", "path/to/dir"
|
||||
// and "/path/to/local" will return ("", "/path/to/local")
|
||||
//
|
||||
// Note that this will turn \ into / in the fsPath on Windows
|
||||
func Parse(path string) (configName, fsPath string) {
|
||||
parts := Matcher.FindStringSubmatch(path)
|
||||
configName, fsPath = "", path
|
||||
if parts != nil && !driveletter.IsDriveLetter(parts[1]) {
|
||||
configName, fsPath = parts[1], parts[2]
|
||||
}
|
||||
// change native directory separators to / if there are any
|
||||
fsPath = filepath.ToSlash(fsPath)
|
||||
return configName, fsPath
|
||||
}
|
||||
|
||||
// Split splits a remote into a parent and a leaf
|
||||
//
|
||||
// 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
|
||||
// (except under Windows where \ will be translated into /)
|
||||
func Split(remote string) (parent string, leaf string) {
|
||||
remoteName, remotePath := Parse(remote)
|
||||
if remoteName != "" {
|
||||
remoteName += ":"
|
||||
}
|
||||
// Construct new remote name without last segment
|
||||
parent, leaf = path.Split(remotePath)
|
||||
return remoteName + parent, leaf
|
||||
}
|
||||
60
.rclone_repo/fs/fspath/path_test.go
Executable file
60
.rclone_repo/fs/fspath/path_test.go
Executable file
@@ -0,0 +1,60 @@
|
||||
package fspath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
in, wantConfigName, wantFsPath string
|
||||
}{
|
||||
{"", "", ""},
|
||||
{"/path/to/file", "", "/path/to/file"},
|
||||
{"path/to/file", "", "path/to/file"},
|
||||
{"remote:path/to/file", "remote", "path/to/file"},
|
||||
{"remote:/path/to/file", "remote", "/path/to/file"},
|
||||
{":backend:/path/to/file", ":backend", "/path/to/file"},
|
||||
} {
|
||||
gotConfigName, gotFsPath := Parse(test.in)
|
||||
assert.Equal(t, test.wantConfigName, gotConfigName)
|
||||
assert.Equal(t, test.wantFsPath, gotFsPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplit(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
remote, wantParent, wantLeaf string
|
||||
}{
|
||||
{"", "", ""},
|
||||
|
||||
{"remote:", "remote:", ""},
|
||||
{"remote:potato", "remote:", "potato"},
|
||||
{"remote:/", "remote:/", ""},
|
||||
{"remote:/potato", "remote:/", "potato"},
|
||||
{"remote:/potato/potato", "remote:/potato/", "potato"},
|
||||
{"remote:potato/sausage", "remote:potato/", "sausage"},
|
||||
|
||||
{":remote:", ":remote:", ""},
|
||||
{":remote:potato", ":remote:", "potato"},
|
||||
{":remote:/", ":remote:/", ""},
|
||||
{":remote:/potato", ":remote:/", "potato"},
|
||||
{":remote:/potato/potato", ":remote:/potato/", "potato"},
|
||||
{":remote:potato/sausage", ":remote:potato/", "sausage"},
|
||||
|
||||
{"/", "/", ""},
|
||||
{"/root", "/", "root"},
|
||||
{"/a/b", "/a/", "b"},
|
||||
{"root", "", "root"},
|
||||
{"a/b", "a/", "b"},
|
||||
{"root/", "root/", ""},
|
||||
{"a/b/", "a/b/", ""},
|
||||
} {
|
||||
gotParent, gotLeaf := Split(test.remote)
|
||||
assert.Equal(t, test.wantParent, gotParent, 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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user