Skip to content

Commit 8f0b4b2

Browse files
add: remotefs/DownloadDirectory function to support recursive download
Signed-off-by: Michael Kaplan <[email protected]>
1 parent 0539c71 commit 8f0b4b2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

remotefs/downloaddirectory.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package remotefs
2+
3+
import (
4+
"fmt"
5+
"io/fs"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
// DownloadDirectory downloads all files and directories recursively from the remote system to local directory.
11+
func DownloadDirectory(fsys FS, src, dst string) error {
12+
walkErr := fs.WalkDir(fsys, src, func(path string, dir fs.DirEntry, err error) error {
13+
if err != nil {
14+
return fmt.Errorf("walk remote directory: %w", err)
15+
}
16+
17+
relPath, err := filepath.Rel(src, path)
18+
if err != nil {
19+
return fmt.Errorf("calculate relative path: %w", err)
20+
}
21+
targetPath := filepath.Join(dst, relPath)
22+
23+
if dir.IsDir() {
24+
dirInfo, err := dir.Info()
25+
if err != nil {
26+
return fmt.Errorf("get dir info: %w", err)
27+
}
28+
if err := os.MkdirAll(targetPath, dirInfo.Mode()&os.ModePerm); err != nil {
29+
return fmt.Errorf("create local directory: %w", err)
30+
}
31+
} else {
32+
if err := Download(fsys, path, targetPath); err != nil {
33+
return fmt.Errorf("download file: %w", err)
34+
}
35+
}
36+
return nil
37+
})
38+
39+
if walkErr != nil {
40+
return fmt.Errorf("walk remote directory tree: %w", walkErr)
41+
}
42+
return nil
43+
}

0 commit comments

Comments
 (0)