Backend: Recursively list directories #225
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
b86f68c3f3
commit
187a80f7e6
2 changed files with 57 additions and 5 deletions
|
@ -32,9 +32,17 @@ func Connect(url, user, pass string) Client {
|
|||
return result
|
||||
}
|
||||
|
||||
func (c Client) readDir(path string) ([]os.FileInfo, error) {
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
return c.client.ReadDir(path)
|
||||
}
|
||||
|
||||
// Files returns all files in path as string slice.
|
||||
func (c Client) Files(path string) (result []string, err error) {
|
||||
files, err := c.client.ReadDir(path)
|
||||
files, err := c.readDir(path)
|
||||
|
||||
if err != nil {
|
||||
return result, err
|
||||
|
@ -49,8 +57,8 @@ func (c Client) Files(path string) (result []string, err error) {
|
|||
}
|
||||
|
||||
// Directories returns all sub directories in path as string slice.
|
||||
func (c Client) Directories(path string) (result []string, err error) {
|
||||
files, err := c.client.ReadDir(path)
|
||||
func (c Client) Directories(path string, recursive bool) (result []string, err error) {
|
||||
files, err := c.readDir(path)
|
||||
|
||||
if err != nil {
|
||||
return result, err
|
||||
|
@ -58,7 +66,19 @@ func (c Client) Directories(path string) (result []string, err error) {
|
|||
|
||||
for _, file := range files {
|
||||
if !file.Mode().IsDir() { continue }
|
||||
result = append(result, fmt.Sprintf("%s/%s", path, file.Name()))
|
||||
|
||||
dir := fmt.Sprintf("%s/%s", path, file.Name())
|
||||
result = append(result, dir)
|
||||
|
||||
if recursive {
|
||||
subDirs, err := c.Directories(dir, true)
|
||||
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
result = append(result, subDirs...)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
@ -118,7 +138,7 @@ func (c Client) DownloadDir(from, to string, recursive bool) (errs []error) {
|
|||
return errs
|
||||
}
|
||||
|
||||
dirs, err := c.Directories(from)
|
||||
dirs, err := c.Directories(from, false)
|
||||
|
||||
for _, dir := range dirs {
|
||||
errs = append(errs, c.DownloadDir(dir, to, true)...)
|
||||
|
|
|
@ -37,6 +37,38 @@ func TestClient_Files(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClient_Directories(t *testing.T) {
|
||||
c := Connect(testUrl, testUser, testPass)
|
||||
|
||||
assert.IsType(t, Client{}, c)
|
||||
|
||||
t.Run("non-recursive", func(t *testing.T) {
|
||||
dirs, err := c.Directories("", false)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(dirs) == 0 {
|
||||
t.Fatal("no directories found")
|
||||
}
|
||||
|
||||
assert.Equal(t, "/Photos", dirs[0])
|
||||
})
|
||||
|
||||
t.Run("recursive", func(t *testing.T) {
|
||||
dirs, err := c.Directories("", true)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(dirs) < 2 {
|
||||
t.Fatal("at least 2 directories expected")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestClient_Download(t *testing.T) {
|
||||
c := Connect(testUrl, testUser, testPass)
|
||||
|
||||
|
|
Loading…
Reference in a new issue