-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: remotefs/DownloadDirectory function to support recursive download
Signed-off-by: Michael Kaplan <[email protected]>
- Loading branch information
1 parent
0539c71
commit 8f0b4b2
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package remotefs | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// DownloadDirectory downloads all files and directories recursively from the remote system to local directory. | ||
func DownloadDirectory(fsys FS, src, dst string) error { | ||
walkErr := fs.WalkDir(fsys, src, func(path string, dir fs.DirEntry, err error) error { | ||
if err != nil { | ||
return fmt.Errorf("walk remote directory: %w", err) | ||
} | ||
|
||
relPath, err := filepath.Rel(src, path) | ||
if err != nil { | ||
return fmt.Errorf("calculate relative path: %w", err) | ||
} | ||
targetPath := filepath.Join(dst, relPath) | ||
|
||
if dir.IsDir() { | ||
dirInfo, err := dir.Info() | ||
if err != nil { | ||
return fmt.Errorf("get dir info: %w", err) | ||
} | ||
if err := os.MkdirAll(targetPath, dirInfo.Mode()&os.ModePerm); err != nil { | ||
return fmt.Errorf("create local directory: %w", err) | ||
} | ||
} else { | ||
if err := Download(fsys, path, targetPath); err != nil { | ||
return fmt.Errorf("download file: %w", err) | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if walkErr != nil { | ||
return fmt.Errorf("walk remote directory tree: %w", walkErr) | ||
} | ||
return nil | ||
} |