Skip to content

Commit

Permalink
copy dir
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Aug 31, 2023
1 parent 4f5449f commit 6f4d83a
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions utils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"golang.org/x/exp/slices"
"io"
Expand Down Expand Up @@ -415,15 +416,15 @@ func CopyDir(fromPath, toPath string, includeDirs bool, excludeNames []string) e
if slices.Contains(excludeNames, filepath.Base(v)) {
continue
}

dir, err := IsDirExists(v, false)
var exists bool
exists, err = IsDirExists(v, false)
if err != nil {
return err
}

if dir {
if exists {
toPath := toPath + GetFileSeparator() + filepath.Base(v)
err := CopyDir(v, toPath, true, nil)
err = CopyDir(v, toPath, true, nil)
if err != nil {
return err
}
Expand All @@ -440,31 +441,29 @@ func CopyDir(fromPath, toPath string, includeDirs bool, excludeNames []string) e
func CopyFile(dst, src string) (err error) {
srcFile, err := os.Open(src)
if err != nil {
return err
return
}
defer func() {
e := srcFile.Close()
if err == nil {
err = e
}
err = errors.Join(err, srcFile.Close())
}()
srcInfo, err := srcFile.Stat()
if err != nil {
return
}
fileName, _ := GetFileAndDirFromPath(src)
dstPath, err := CreateFilePath(dst, fileName)
if err != nil {
return err
return
}
dstFile, err := os.Create(dstPath)
dstFile, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())
if err != nil {
return err
return
}
defer func() {
e := dstFile.Close()
if err == nil {
err = e
}
err = errors.Join(err, dstFile.Close())
}()
_, err = io.Copy(dstFile, srcFile)
return err
return
}

func GetFileSeparator() string {
Expand Down

0 comments on commit 6f4d83a

Please sign in to comment.