prototype

This commit is contained in:
2025-08-20 21:15:35 +03:00
parent 598d516b57
commit 58414744ce
11 changed files with 390 additions and 0 deletions

55
internal/utils/fsutils.go Normal file
View File

@@ -0,0 +1,55 @@
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)
}
})
}

View File

@@ -0,0 +1,20 @@
package utils
import (
"io"
"os"
)
func IsDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err // Either not empty or error, suits both cases
}