2020-06-19 14:12:29 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CopyDir(fs afero.Fs, src string, dst string) error {
|
|
|
|
var err error
|
2021-12-15 14:35:04 +00:00
|
|
|
var fds []os.DirEntry
|
2020-06-19 14:12:29 +00:00
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcinfo, err = fs.Stat(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fs.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-15 14:35:04 +00:00
|
|
|
if fds, err = os.ReadDir(src); err != nil {
|
2020-06-19 14:12:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, fd := range fds {
|
|
|
|
srcPath := path.Join(src, fd.Name())
|
|
|
|
dstPath := path.Join(dst, fd.Name())
|
|
|
|
|
|
|
|
if fd.IsDir() {
|
|
|
|
if err = CopyDir(fs, srcPath, dstPath); err != nil {
|
|
|
|
return fmt.Errorf("could not copy dir (%s -> %s): %w", srcPath, dstPath, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = CopyFile(fs, srcPath, dstPath); err != nil {
|
|
|
|
return fmt.Errorf("could not copy file (%s -> %s): %w", srcPath, dstPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopyFile(fs afero.Fs, src, dst string) error {
|
|
|
|
var err error
|
|
|
|
var srcFd afero.File
|
|
|
|
var dstFd afero.File
|
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcFd, err = fs.Open(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFd.Close()
|
|
|
|
|
|
|
|
if dstFd, err = fs.Create(dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFd.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dstFd, srcFd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if srcinfo, err = fs.Stat(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fs.Chmod(dst, srcinfo.Mode())
|
|
|
|
}
|