Skip to content

Commit 1ceb1bd

Browse files
committed
fix: better error handling around existing files
1 parent 8e9158c commit 1ceb1bd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

fs.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ func (fm *FileManager) ValidateDestDir() error {
1515
dstInfo, err := os.Stat(fm.DestDir)
1616
if err != nil {
1717
if os.IsNotExist(err) {
18-
return os.MkdirAll(fm.DestDir, 0755)
18+
// if destination does not exist, skip validation since the given
19+
// destination will be created in `fm.MoveFiles`. if created here,
20+
// `os.Rename` operation will fail due to an existing newpath
21+
return nil
1922
}
2023
return err
2124
}
@@ -40,7 +43,11 @@ func (fm *FileManager) MoveFiles(sources []string, tempDir string) error {
4043
dst := filepath.Join(fm.DestDir, ford)
4144
if err := os.Rename(src, dst); err != nil {
4245
if errors.Is(err, os.ErrExist) {
43-
// overwrite (delete) existing directory here
46+
// if options.Force:
47+
// — delete existing directory dst
48+
// - return nil to continue with overwrite
49+
// else:
50+
return fmt.Errorf("directory with content at `%s' would be overwritten, skipping…", dst)
4451
}
4552
return err
4653
}

0 commit comments

Comments
 (0)