-
Notifications
You must be signed in to change notification settings - Fork 31
/
markup_file_finder.go
74 lines (59 loc) · 1.14 KB
/
markup_file_finder.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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
)
type markupFileFinder struct {
filenames chan string
errors chan error
}
func newMarkupFileFinder() markupFileFinder {
return markupFileFinder{
make(chan string, maxOpenFiles),
make(chan error, 64),
}
}
func (m markupFileFinder) Filenames() chan string {
return m.filenames
}
func (m markupFileFinder) Errors() chan error {
return m.errors
}
func (m markupFileFinder) Find(fs []string, recursive bool) {
for _, f := range fs {
i, err := os.Stat(f)
if err != nil {
m.errors <- err
continue
}
if i.IsDir() && recursive {
m.listDirectory(f)
} else if i.IsDir() {
m.errors <- fmt.Errorf("%v is not a file", f)
} else {
m.filenames <- f
}
}
close(m.filenames)
close(m.errors)
}
func (m markupFileFinder) listDirectory(d string) {
err := filepath.Walk(d, func(f string, i os.FileInfo, err error) error {
if err != nil {
return err
}
b, err := regexp.MatchString("(^\\.)|(/\\.)", f)
if err != nil {
return err
}
if !i.IsDir() && !b && isMarkupFile(f) {
m.filenames <- f
}
return nil
})
if err != nil {
m.errors <- err
}
}