-
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/UploadDirectory function to support recursive upload
Signed-off-by: Michael Kaplan <michael@kaplan.sh>
- Loading branch information
1 parent
d92b3db
commit 0539c71
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" | ||
) | ||
|
||
// UploadDirectory uploads all files and directories recursively to the remote system. | ||
func UploadDirectory(fsys FS, src, dst string) error { | ||
walkErr := filepath.WalkDir(src, func(path string, dir fs.DirEntry, err error) error { | ||
if err != nil { | ||
return fmt.Errorf("walk local 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 := fsys.MkdirAll(targetPath, dirInfo.Mode()&os.ModePerm); err != nil { | ||
return fmt.Errorf("create remote directory: %w", err) | ||
} | ||
} else { | ||
if err := Upload(fsys, path, targetPath); err != nil { | ||
return fmt.Errorf("upload file: %w", err) | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if walkErr != nil { | ||
return fmt.Errorf("walk remote directory tree: %w", walkErr) | ||
} | ||
return nil | ||
} |