This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
file_system.go
85 lines (76 loc) · 1.74 KB
/
file_system.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
84
85
package godm
import (
"io/ioutil"
"os"
"path"
"regexp"
"strings"
shutil "github.com/termie/go-shutil"
)
func listGoFiles(currentPath string, recursive bool, excludes []*regexp.Regexp, firstLevel bool) (Set, error) {
for _, excludeRegexp := range excludes {
if excludeRegexp.MatchString(currentPath) {
return nil, nil
}
}
file, err := os.Stat(currentPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
if !file.IsDir() {
if strings.HasSuffix(file.Name(), ".go") {
return NewSet(currentPath), nil
} else {
return nil, nil
}
}
if recursive || firstLevel {
children, err := ioutil.ReadDir(currentPath)
if err != nil {
return nil, err
}
set := NewSet()
for _, child := range children {
childSet, err := listGoFiles(path.Join(currentPath, child.Name()), recursive, excludes, false)
if err != nil {
return nil, err
}
set.AddSet(childSet)
}
return set, nil
}
return nil, nil
}
var copyTreeOptions = &shutil.CopyTreeOptions{
Symlinks: true,
IgnoreDanglingSymlinks: true,
}
func CopyDir(src, target string) error {
return shutil.CopyTree(src, target, copyTreeOptions)
}
func RemoveSubdirsWithNoFiles(dirPath string) (hasAtLeastOneFile bool, err error) {
var fileInfos []os.FileInfo
fileInfos, err = ioutil.ReadDir(dirPath)
if err != nil {
return
}
var subdirHasAtLeastOneFile bool
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
subdirHasAtLeastOneFile, err = RemoveSubdirsWithNoFiles(path.Join(dirPath, fileInfo.Name()))
hasAtLeastOneFile = hasAtLeastOneFile || subdirHasAtLeastOneFile
if err != nil {
return
}
} else {
hasAtLeastOneFile = true
}
}
if !hasAtLeastOneFile {
os.RemoveAll(dirPath)
}
return
}