mirror of
https://github.com/rclone/rclone.git
synced 2025-08-10 06:09:44 +02:00
Break the fs package up into smaller parts.
The purpose of this is to make it easier to maintain and eventually to allow the rclone backends to be re-used in other projects without having to use the rclone configuration system. The new code layout is documented in CONTRIBUTING.
This commit is contained in:
31
fs/fspath/path.go
Normal file
31
fs/fspath/path.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Package fspath contains routines for fspath manipulation
|
||||
package fspath
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RemoteSplit 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
|
||||
func RemoteSplit(remote string) (parent string, leaf string) {
|
||||
// Split remote on :
|
||||
i := strings.Index(remote, ":")
|
||||
remoteName := ""
|
||||
remotePath := remote
|
||||
if i >= 0 {
|
||||
remoteName = remote[:i+1]
|
||||
remotePath = remote[i+1:]
|
||||
} else if strings.HasSuffix(remotePath, "/") {
|
||||
// if no : and ends with / must be directory
|
||||
return remotePath, ""
|
||||
}
|
||||
// Construct new remote name without last segment
|
||||
parent, leaf = path.Split(remotePath)
|
||||
return remoteName + parent, leaf
|
||||
}
|
35
fs/fspath/path_test.go
Normal file
35
fs/fspath/path_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package fspath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRemoteSplit(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"},
|
||||
{"/", "/", ""},
|
||||
{"/root", "/", "root"},
|
||||
{"/a/b", "/a/", "b"},
|
||||
{"root", "", "root"},
|
||||
{"a/b", "a/", "b"},
|
||||
{"root/", "root/", ""},
|
||||
{"a/b/", "a/b/", ""},
|
||||
} {
|
||||
gotParent, gotLeaf := RemoteSplit(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