Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 14s
CI Workflow / CI run build and linting (pull_request) Failing after 10s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / CI run tests (pull_request) Failing after 57s
131 lines
2.5 KiB
Go
131 lines
2.5 KiB
Go
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
)
|
|
|
|
// Source - https://stackoverflow.com/a
|
|
// Posted by Oleg Neumyvakin, modified by community. See post 'Timeline' for change history
|
|
// Retrieved 2026-01-20, License - CC BY-SA 4.0
|
|
|
|
func CopyDirectory(scrDir, dest string) error {
|
|
entries, err := os.ReadDir(scrDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, entry := range entries {
|
|
sourcePath := filepath.Join(scrDir, entry.Name())
|
|
destPath := filepath.Join(dest, entry.Name())
|
|
|
|
fileInfo, err := os.Stat(sourcePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
stat, ok := fileInfo.Sys().(*syscall.Stat_t)
|
|
if !ok {
|
|
return fmt.Errorf("failed to get raw syscall.Stat_t data for '%s'", sourcePath)
|
|
}
|
|
|
|
switch fileInfo.Mode() & os.ModeType {
|
|
case os.ModeDir:
|
|
if err := CreateIfNotExists(destPath, 0o755); err != nil {
|
|
return err
|
|
}
|
|
if err := CopyDirectory(sourcePath, destPath); err != nil {
|
|
return err
|
|
}
|
|
case os.ModeSymlink:
|
|
if err := CopySymLink(sourcePath, destPath); err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
if err := Copy(sourcePath, destPath); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := os.Lchown(destPath, int(stat.Uid), int(stat.Gid)); err != nil {
|
|
return err
|
|
}
|
|
|
|
fInfo, err := entry.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
isSymlink := fInfo.Mode()&os.ModeSymlink != 0
|
|
if !isSymlink {
|
|
if err := os.Chmod(destPath, fInfo.Mode()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Copy(srcFile, dstFile string) error {
|
|
out, err := os.Create(dstFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func(out *os.File) {
|
|
err := out.Close()
|
|
if err != nil {
|
|
slog.Error("failed to close file", slog.Any("err", err))
|
|
}
|
|
}(out)
|
|
|
|
in, err := os.Open(srcFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func(in *os.File) {
|
|
err := in.Close()
|
|
if err != nil {
|
|
slog.Error("error closing destination file", slog.Any("err", err))
|
|
}
|
|
}(in)
|
|
|
|
_, err = io.Copy(out, in)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Exists(filePath string) bool {
|
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func CreateIfNotExists(dir string, perm os.FileMode) error {
|
|
if Exists(dir) {
|
|
return nil
|
|
}
|
|
|
|
if err := os.MkdirAll(dir, perm); err != nil {
|
|
return fmt.Errorf("failed to create directory: '%s', error: '%s'", dir, err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CopySymLink(source, dest string) error {
|
|
link, err := os.Readlink(source)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Symlink(link, dest)
|
|
}
|