Skip to content

Commit 0539c71

Browse files
add: remotefs/UploadDirectory function to support recursive upload
Signed-off-by: Michael Kaplan <[email protected]>
1 parent d92b3db commit 0539c71

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

remotefs/uploaddirectory.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+
// UploadDirectory uploads all files and directories recursively to the remote system.
11+
func UploadDirectory(fsys FS, src, dst string) error {
12+
walkErr := filepath.WalkDir(src, func(path string, dir fs.DirEntry, err error) error {
13+
if err != nil {
14+
return fmt.Errorf("walk local 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 := fsys.MkdirAll(targetPath, dirInfo.Mode()&os.ModePerm); err != nil {
29+
return fmt.Errorf("create remote directory: %w", err)
30+
}
31+
} else {
32+
if err := Upload(fsys, path, targetPath); err != nil {
33+
return fmt.Errorf("upload 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)