Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit 0323bcd

Browse files
committed
Add copyDir fuction
1 parent c90aef5 commit 0323bcd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

utils.go

+38
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"io/ioutil"
2828
"os"
2929
"os/exec"
30+
"path"
3031
"regexp"
3132
"strconv"
3233
"strings"
@@ -82,6 +83,43 @@ func CopyFile(srcPath, destPath string) error {
8283
return nil
8384
}
8485

86+
// CopyDir copies a whole directory recursively
87+
// snippet from https://blog.depa.do/post/copy-files-and-directories-in-go
88+
func CopyDir(srcPath string, dstPath string) error {
89+
var err error
90+
var fds []os.FileInfo
91+
var srcinfo os.FileInfo
92+
93+
srcinfo, err = os.Stat(srcPath)
94+
if err != nil {
95+
return err
96+
}
97+
98+
err = os.MkdirAll(dstPath, srcinfo.Mode())
99+
if err != nil {
100+
return err
101+
}
102+
103+
if fds, err = ioutil.ReadDir(srcPath); err != nil {
104+
return err
105+
}
106+
for _, fd := range fds {
107+
srcfp := path.Join(srcPath, fd.Name())
108+
dstfp := path.Join(srcPath, fd.Name())
109+
110+
if fd.IsDir() {
111+
if err = CopyDir(srcfp, dstfp); err != nil {
112+
return err
113+
}
114+
} else {
115+
if err = CopyFile(srcfp, dstfp); err != nil {
116+
return err
117+
}
118+
}
119+
}
120+
return nil
121+
}
122+
85123
// CopyFileReplace copies a file within the snap and replaces strings using
86124
// the string/replace values in the rStrings parameter.
87125
func CopyFileReplace(srcPath, destPath string, rStrings map[string]string) error {

0 commit comments

Comments
 (0)