-
Notifications
You must be signed in to change notification settings - Fork 1
/
walk.go
81 lines (74 loc) · 2.13 KB
/
walk.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
package nogo
import (
"io/fs"
)
// WalkFunc can be used in any Walk function to automatically ignore ignored files.
// It is similar to ForWalkDir but with it you can write a WalkFunc for any other (than fs.WalkDir) Walk function.
// It returns true if everything is ok and false if the path is ignored and should be skipped.
//
//
// You have to call AddFromFS with the same fs before running the walk!
//
// The Walk function you use must support the fs.SkipDir error (or you have to skip that manually)
//
// Example for afero:
// if err := n.AddFromFS(walkFS, ".gitignore"); err != nil {
// panic(err)
// }
//
// err = afero.Walk(baseFS, ".", func(path string, info fs.FileInfo, err error) error {
// if ok, err := n.WalkFunc(afero.NewIOFS(baseFS), path, info.IsDir(), err); !ok {
// return err
// }
//
// fmt.Println(path, info.Name())
// return nil
// })
func (n *NoGo) WalkFunc(fsys fs.FS, path string, isDir bool, err error) (bool, error) {
if err != nil {
return false, err
}
if path != "." {
if match, _ := n.MatchWithoutParents(path, isDir); match {
if isDir {
return false, fs.SkipDir
}
return false, nil
}
}
return true, nil
}
// ForWalkDir can be used to set all parameters of fs.WalkDir.
// It only calls the passed WalkDirFunc for files and directories
// which are not ignored.
//
// You have to call AddFromFS with the same fs before running the walk!
//
// If you need something similar for any other Walk function (e.g. afero.Walk)
// You can use WalkFunc for that.
//
// Example:
// if err := n.AddFromFS(walkFS, ".gitignore"); err != nil {
// panic(err)
// }
//
// n := nogo.New(nogo.DotGitRule)
// err = fs.WalkDir(n.ForWalkDir(walkFS, ".", func(path string, d fs.DirEntry, err error) error {
// if err != nil {
// return err
// }
// fmt.Println(path, d.Name())
// return nil
// }))
func (n *NoGo) ForWalkDir(fsys fs.FS, root string, fn fs.WalkDirFunc) (fs.FS, string, fs.WalkDirFunc) {
return fsys, root, func(path string, d fs.DirEntry, err error) error {
ok, err := n.WalkFunc(fsys, path, d.IsDir(), err)
if err != nil {
return err
}
if ok {
return fn(path, d, err)
}
return nil
}
}