-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilesystem.go
More file actions
73 lines (58 loc) · 1.61 KB
/
filesystem.go
File metadata and controls
73 lines (58 loc) · 1.61 KB
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
package collection
import (
"context"
"os"
"path/filepath"
)
// DirectoryOptions is a means of configuring a `Directory` instance to including various children in its enumeration without
// supplying a `Where` clause later.
type DirectoryOptions uint
// These constants define all of the supported options for configuring a `Directory`
const (
DirectoryOptionsExcludeFiles = 1 << iota
DirectoryOptionsExcludeDirectories
DirectoryOptionsRecursive
)
// Directory treats a filesystem path as a collection of filesystem entries, specifically a collection of directories and files.
type Directory struct {
Location string
Options DirectoryOptions
}
func (d Directory) applyOptions(loc string, info os.FileInfo) bool {
if info.IsDir() && (d.Options&DirectoryOptionsExcludeDirectories) != 0 {
return false
}
if !info.IsDir() && d.Options&DirectoryOptionsExcludeFiles != 0 {
return false
}
return true
}
// Enumerate lists the items in a `Directory`
func (d Directory) Enumerate(ctx context.Context) Enumerator[string] {
results := make(chan string)
go func() {
defer close(results)
filepath.Walk(d.Location, func(currentLocation string, info os.FileInfo, openErr error) (err error) {
if openErr != nil {
err = openErr
return
}
if d.Location == currentLocation {
return
}
if info.IsDir() && d.Options&DirectoryOptionsRecursive == 0 {
err = filepath.SkipDir
}
if d.applyOptions(currentLocation, info) {
select {
case results <- currentLocation:
// Intentionally Left Blank
case <-ctx.Done():
err = ctx.Err()
}
}
return
})
}()
return results
}