|
@@ -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)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|