- package Filesystem
-
- import (
- "io/fs"
- "io/ioutil"
- "os"
- "path/filepath"
- )
-
- func CopyFile(src, dest string) error {
- var (
- input []byte
- fileInfo fs.FileInfo
- srcBasePath string
- destBasePath string
- err error
- )
-
- srcBasePath = filepath.Dir(src)
- destBasePath = filepath.Dir(dest)
-
- fileInfo, err = os.Stat(srcBasePath)
- if err != nil {
- return err
- }
-
- err = os.MkdirAll(destBasePath, fileInfo.Mode())
- if err != nil {
- return err
- }
-
- fileInfo, err = os.Stat(src)
- if err != nil {
- return err
- }
-
- input, err = ioutil.ReadFile(src)
- if err != nil {
- return err
- }
-
- err = ioutil.WriteFile(dest, input, fileInfo.Mode())
- if err != nil {
- return err
- }
-
- return nil
- }
|