-
Notifications
You must be signed in to change notification settings - Fork 10
/
exists.go
83 lines (72 loc) · 1.48 KB
/
exists.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"io"
"io/fs"
"os"
"path/filepath"
)
func FileExists(name string) bool {
fi, err := os.Stat(name)
if err != nil {
return false
}
if fi.IsDir() {
return false
}
return true
}
func DirExists(name string) bool {
fi, err := os.Stat(name)
if err != nil {
return false
}
if fi.IsDir() {
return true
}
return false
}
// CopyDir recursively copies all files and directories from src to dst.
func CopyDir(src, dst string) error {
return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// Define the target path by replacing the src base with dst
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
targetPath := filepath.Join(dst, relPath)
// If it's a directory, create it in the destination
if d.IsDir() {
return os.MkdirAll(targetPath, d.Type().Perm())
}
// Copy the file
return copyFile(path, targetPath)
})
}
// copyFile copies a single file from src to dst.
func copyFile(src, dst string) error {
// Open the source file
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// Create the destination file
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
// Copy the file content
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
// Copy file permissions
srcInfo, err := srcFile.Stat()
if err != nil {
return err
}
return os.Chmod(dst, srcInfo.Mode())
}