Skip to content

Commit

Permalink
add: remotefs/DownloadDirectory function to support recursive download
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplan-michael committed May 4, 2024
1 parent 66ca9bc commit 54e8e2d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions remotefs/downloaddirectory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package remotefs

Check failure on line 1 in remotefs/downloaddirectory.go

View workflow job for this annotation

GitHub Actions / Lint

1-49 lines are duplicate of `remotefs/uploaddirectory.go:1-49` (dupl)

import (
"fmt"
"os"
"path/filepath"
)

// DownloadDirectory downloads all files and directories recursively from the remote system to local directory.
func DownloadDirectory(fs FS, src, dst string) error {
// Ensure the destination directory is created even if empty
srcInfo, err := fs.Stat(src)
if err != nil {
return fmt.Errorf("stat dir for download: %w", err)
}
mode := srcInfo.Mode() & os.ModePerm
if err := os.MkdirAll(dst, mode); err != nil {
return fmt.Errorf("create empty dir for download: %w", err)
}

entries, err := fs.ReadDir(src)
if err != nil {
return fmt.Errorf("read dir for download: %w", err)
}

for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())

info, err := entry.Info()
if err != nil {
return fmt.Errorf("dir info for download: %w", err)
}

if info.IsDir() {
if err := os.MkdirAll(dstPath, info.Mode()); err != nil {
return fmt.Errorf("create dir for download: %w", err)
}
if err := DownloadDirectory(fs, srcPath, dstPath); err != nil {
return fmt.Errorf("download dir: %w", err)
}
} else {
if err := Download(fs, srcPath, dstPath); err != nil {
return fmt.Errorf("download file in dir: %w", err)
}
}
}
return nil
}

0 comments on commit 54e8e2d

Please sign in to comment.