-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
65 lines (55 loc) · 1.25 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package filex
import (
"os"
"path/filepath"
"strings"
)
// IsDir checks if the given path is a directory.
func IsDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
// IfIsDir executes the given functionx if the given path is a directory.
// an error is returned if execution fails.
func IfIsDir(path string, f func() error) error {
if IsDir(path) {
return f()
}
return nil
}
func IsEmptyDir(path string) bool {
if !IsDir(path) {
return false
}
files, err := os.ReadDir(path)
if err != nil {
return false
}
return len(files) == 0
}
// DeleteDir deletes a directory.
func DeleteDir(dir string) error {
return os.RemoveAll(dir)
}
// DeleteDirFn executes the functionx if the directory is deleted.
func DeleteDirFn(dir string, fn func() error) error {
if err := DeleteDir(dir); err != nil {
return err
}
return fn()
}
// IsSubDir checks if the given path is a subdirectory of the given parent.
func IsSubDir(parent, child string) bool {
relPath, err := filepath.Rel(parent, child)
if err != nil {
return false
}
if strings.Contains(parent, child) {
n := strings.ReplaceAll(child, parent, "")
return filepath.Join(parent, relPath) == filepath.Join(parent, n)
}
return false
}