forked from nathany/looper
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsubfolders.go
38 lines (34 loc) · 949 Bytes
/
subfolders.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
package recwatch
import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/xyproto/symwalk"
)
// ShouldIgnoreFile determines if a file should be ignored.
// File names that begin with "." or "_" are ignored by the go tool.
func ShouldIgnoreFile(name string) bool {
return strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_")
}
// Subfolders returns a slice of subfolders (recursive), including the provided path
func Subfolders(path string) (paths []string) {
var mut sync.Mutex
symwalk.Walk(path, func(newPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
name := info.Name()
// Skip folders that begin with "_" or "."
if ShouldIgnoreFile(name) {
return filepath.SkipDir // returns to filepath.Walk
}
mut.Lock()
paths = append(paths, newPath)
mut.Unlock()
}
return nil // returns to filepath.Walk
})
return paths // return collected paths
}