-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathcopy_dir.go
47 lines (40 loc) · 827 Bytes
/
copy_dir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"os"
"path/filepath"
)
// it would be really nice if there were a standard Go fileutils package...
func copyCodePackageDir(src, dst string) error {
err := os.MkdirAll(dst, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, e := range entries {
srcPath := filepath.Join(src, e.Name())
dstPath := filepath.Join(dst, e.Name())
if e.IsDir() {
err := copyCodePackageDir(srcPath, dstPath)
if err != nil {
return err
}
} else {
data, err := os.ReadFile(srcPath)
if err != nil {
return err
}
fileInfo, err := os.Stat(srcPath)
if err != nil {
return err
}
err = os.WriteFile(dstPath, data, fileInfo.Mode().Perm())
if err != nil {
return err
}
}
}
return nil
}