mukan-ignite/ignite/pkg/xos/cp.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

131 lines
2.9 KiB
Go

package xos
import (
"io"
"os"
"path/filepath"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
)
// CopyFolder copy the source folder to the destination folder.
func CopyFolder(srcPath, dstPath string) error {
return filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip the root folder
if path == srcPath {
return nil
}
// Get the relative path within the source folder
relativePath, err := filepath.Rel(srcPath, path)
if err != nil {
return err
}
// Create the corresponding destination path
destPath := filepath.Join(dstPath, relativePath)
if info.IsDir() {
// Create the directory in the destination
err = os.MkdirAll(destPath, 0o755)
if err != nil {
return err
}
} else {
// Copy the file content
err = CopyFile(path, destPath)
if err != nil {
return err
}
}
return nil
})
}
// ValidateFolderCopy validates that all files in source folder exist in destination folder
// with same name and relative path.
func ValidateFolderCopy(srcPath, dstPath string, exclude ...string) ([]string, error) {
if srcPath == dstPath {
return nil, errors.Errorf("source and destination paths are the same %s", srcPath)
}
// Check if the destination path exists
if _, err := os.Stat(dstPath); errors.Is(err, os.ErrNotExist) {
return nil, errors.Errorf("destination path does not exist: %s", dstPath)
} else if err != nil {
return nil, err
}
excludeMap := make(map[string]struct{}, len(exclude))
for _, ex := range exclude {
excludeMap[ex] = struct{}{}
}
var sameFiles []string
err := filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error {
if errors.Is(err, os.ErrNotExist) {
return errors.Errorf("source path does not exist: %s", path)
}
if err != nil {
return err
}
// Skip dirs
if info.IsDir() {
return nil
}
// Get the relative path within the source folder
relativePath, err := filepath.Rel(srcPath, path)
if err != nil {
return err
}
// Skip excluded files
if _, ok := excludeMap[relativePath]; ok {
return nil
}
// Create the corresponding destination path
destPath := filepath.Join(dstPath, relativePath)
// Check if the destination path exists
destInfo, err := os.Stat(destPath)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
// Verify if directory/file types match
if info.IsDir() != destInfo.IsDir() {
return os.ErrInvalid
}
sameFiles = append(sameFiles, relativePath)
return nil
})
return sameFiles, err
}
// CopyFile copy the source file to the destination file.
func CopyFile(srcPath, dstPath string) error {
srcFile, err := os.OpenFile(srcPath, os.O_RDONLY, 0o666)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(dstPath)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
return err
}