@@ -8,35 +8,35 @@ import (
8
8
)
9
9
10
10
type filesystemWalker struct {
11
- root string
12
- pathsCh chan string
11
+ root string
12
+ pathsCh chan string
13
+ relPathOffset int
13
14
}
14
15
15
16
func (f filesystemWalker ) Root () string {
16
17
return f .root
17
18
}
18
19
19
- func (f filesystemWalker ) Walk (_ context.Context , fn WalkFunc ) error {
20
- relPathOffset := len (f .root ) + 1
21
-
22
- relPathFn := func (path string ) (string , error ) {
23
- // quick optimization for the majority of use cases
24
- if len (path ) >= relPathOffset && path [:len (f .root )] == f .root {
25
- return path [relPathOffset :], nil
26
- }
27
- // fallback to proper relative path resolution
28
- return filepath .Rel (f .root , path )
20
+ func (f filesystemWalker ) relPath (path string ) (string , error ) {
21
+ // quick optimization for the majority of use cases
22
+ if len (path ) >= f .relPathOffset && path [:len (f .root )] == f .root {
23
+ return path [f .relPathOffset :], nil
29
24
}
25
+ // fallback to proper relative path resolution
26
+ return filepath .Rel (f .root , path )
27
+ }
30
28
29
+ func (f filesystemWalker ) Walk (_ context.Context , fn WalkFunc ) error {
31
30
walkFn := func (path string , info fs.FileInfo , _ error ) error {
32
31
if info == nil {
33
32
return fmt .Errorf ("no such file or directory '%s'" , path )
34
33
}
35
34
36
- relPath , err := relPathFn (path )
35
+ relPath , err := f . relPath (path )
37
36
if err != nil {
38
37
return fmt .Errorf ("failed to determine a relative path for %s: %w" , path , err )
39
38
}
39
+
40
40
file := File {
41
41
Path : path ,
42
42
RelPath : relPath ,
@@ -55,5 +55,9 @@ func (f filesystemWalker) Walk(_ context.Context, fn WalkFunc) error {
55
55
}
56
56
57
57
func NewFilesystem (root string , paths chan string ) (Walker , error ) {
58
- return filesystemWalker {root , paths }, nil
58
+ return filesystemWalker {
59
+ root : root ,
60
+ pathsCh : paths ,
61
+ relPathOffset : len (root ) + 1 ,
62
+ }, nil
59
63
}
0 commit comments