fix override data folder

This commit is contained in:
Help-14 2022-04-19 18:38:22 +07:00
parent 702cb9c402
commit d4e90af473
2 changed files with 15 additions and 34 deletions

View file

@ -50,11 +50,8 @@ func prepare() {
pwd, _ = os.Getwd()
dataPath := filepath.Join(pwd, "data")
if _, err := os.Stat(filepath.Join(dataPath, "data.yaml")); os.IsNotExist(err) {
log.Println("Copy files to data folder")
os.MkdirAll(dataPath, os.ModePerm)
modules.CopyDir(filepath.Join(pwd, "common"), dataPath)
}
os.MkdirAll(dataPath, os.ModePerm)
modules.CopyDir(filepath.Join(pwd, "common"), dataPath, false)
}
func loadData() {

View file

@ -6,30 +6,12 @@ import (
"os"
)
// func CopyFile(src, dst string) {
// fin, err := os.Open(src)
// if err != nil {
// log.Fatal(err)
// }
// defer fin.Close()
// fout, err := os.Create(dst)
// if err != nil {
// log.Fatal(err)
// }
// defer fout.Close()
// _, err = io.Copy(fout, fin)
// if err != nil {
// log.Fatal(err)
// }
// }
// func CopyDirectory(oldDir, newDir string) {
// cmd := exec.Command("cp", "--recursive", oldDir, newDir)
// cmd.Run()
// }
func Exists(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}
func CopyFile(source string, dest string) (err error) {
sourcefile, err := os.Open(source)
@ -57,7 +39,7 @@ func CopyFile(source string, dest string) (err error) {
return
}
func CopyDir(source string, dest string) (err error) {
func CopyDir(source string, dest string, override bool) (err error) {
// get properties of source dir
sourceinfo, err := os.Stat(source)
if err != nil {
@ -77,15 +59,17 @@ func CopyDir(source string, dest string) (err error) {
destinationfilepointer := dest + "/" + obj.Name()
if obj.IsDir() {
// create sub-directories - recursively
err = CopyDir(sourcefilepointer, destinationfilepointer)
err = CopyDir(sourcefilepointer, destinationfilepointer, override)
if err != nil {
fmt.Println(err)
}
} else {
// perform copy
err = CopyFile(sourcefilepointer, destinationfilepointer)
if err != nil {
fmt.Println(err)
if Exists(destinationfilepointer) && override {
err = CopyFile(sourcefilepointer, destinationfilepointer)
if err != nil {
fmt.Println(err)
}
}
}
}