2020-02-24 19:14:46 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-10-28 12:31:03 +03:00
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2020-02-24 19:14:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func FSCheckIfDirPathExists(path string) error {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.FSCheckIfDirPathExists"
|
2020-02-24 19:14:46 +03:00
|
|
|
stat, err := os.Lstat(path)
|
|
|
|
if err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
2022-10-28 12:31:03 +03:00
|
|
|
err = errors.E(op, "no such directory")
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func FSCopyFile(src, dst string) error {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.FSCopyFile"
|
2020-02-24 19:14:46 +03:00
|
|
|
var err error
|
|
|
|
var srcfd *os.File
|
|
|
|
var dstfd *os.File
|
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcfd, err = os.Open(src); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
defer srcfd.Close()
|
|
|
|
|
|
|
|
if dstfd, err = os.Create(dst); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
defer dstfd.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dstfd, srcfd); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
if srcinfo, err = os.Stat(src); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
|
|
|
}
|
|
|
|
err = os.Chmod(dst, srcinfo.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-10-28 12:31:03 +03:00
|
|
|
return nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func FSCopyDir(src string, dst string) error {
|
2022-10-28 12:31:03 +03:00
|
|
|
var op errors.Op = "util.FSCopyDir"
|
2020-02-24 19:14:46 +03:00
|
|
|
var err error
|
|
|
|
var fds []os.FileInfo
|
|
|
|
var srcinfo os.FileInfo
|
|
|
|
|
|
|
|
if srcinfo, err = os.Stat(src); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if fds, err = ioutil.ReadDir(src); err != nil {
|
2022-10-28 12:31:03 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
for _, fd := range fds {
|
|
|
|
srcfp := path.Join(src, fd.Name())
|
|
|
|
dstfp := path.Join(dst, fd.Name())
|
|
|
|
|
|
|
|
if fd.IsDir() {
|
|
|
|
if err = FSCopyDir(srcfp, dstfp); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = FSCopyFile(srcfp, dstfp); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|