Files
old_carga/internal/utils/fsutils.go
2025-08-20 21:15:35 +03:00

56 lines
1.0 KiB
Go

package utils
import (
"io"
"log"
"os"
"path/filepath"
)
func CopyFile(src, dst string) error {
log.Printf("копирую %s в %s", src, dst)
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
// create destination folder if not exist
if err = os.MkdirAll(filepath.Dir(dst), os.ModePerm); err != nil {
return err
}
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
return err
}
// CopyDir recursively copies a directory tree
func CopyDir(src string, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Construct new path
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
targetPath := filepath.Join(dst, relPath)
if info.IsDir() {
// create directory
return os.MkdirAll(targetPath, info.Mode())
} else {
// copy file
return CopyFile(path, targetPath)
}
})
}