From d30cc1e11917a00fce11f34d75b359f34fcbe04f Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 7 Jun 2017 12:27:33 +0100 Subject: [PATCH] Factor RemoteSplit into fs --- cmd/cmd.go | 27 +-------------------------- fs/path.go | 30 ++++++++++++++++++++++++++++++ cmd/cmd_test.go => fs/path_test.go | 2 +- 3 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 fs/path.go rename cmd/cmd_test.go => fs/path_test.go (98%) diff --git a/cmd/cmd.go b/cmd/cmd.go index 82a01c8b3..e2bc2cc0a 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -14,7 +14,6 @@ import ( "regexp" "runtime" "runtime/pprof" - "strings" "time" "github.com/spf13/cobra" @@ -166,30 +165,6 @@ func NewFsSrcDst(args []string) (fs.Fs, fs.Fs) { return fsrc, fdst } -// 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 -} - // NewFsSrcDstFiles creates a new src and dst fs from the arguments // If src is a file then srcFileName and dstFileName will be non-empty func NewFsSrcDstFiles(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs, dstFileName string) { @@ -197,7 +172,7 @@ func NewFsSrcDstFiles(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs // If copying a file... dstRemote := args[1] if srcFileName != "" { - dstRemote, dstFileName = RemoteSplit(dstRemote) + dstRemote, dstFileName = fs.RemoteSplit(dstRemote) if dstRemote == "" { dstRemote = "." } diff --git a/fs/path.go b/fs/path.go new file mode 100644 index 000000000..1167e28c2 --- /dev/null +++ b/fs/path.go @@ -0,0 +1,30 @@ +package fs + +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 +} diff --git a/cmd/cmd_test.go b/fs/path_test.go similarity index 98% rename from cmd/cmd_test.go rename to fs/path_test.go index 126350d9c..cf455d031 100644 --- a/cmd/cmd_test.go +++ b/fs/path_test.go @@ -1,4 +1,4 @@ -package cmd +package fs import ( "fmt"