-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_list.go
58 lines (48 loc) · 939 Bytes
/
file_list.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
package traverse
type FileListRes struct {
List []string
Map map[string]struct{}
ch chan string
*option
over chan struct{}
}
func (s *FileListRes) callbackForGetFileList(item *TraverseItem) {
if s.option.TargetExt != "" {
if item.Ext == s.option.TargetExt {
s.ch <- item.Path
}
}
}
func (s *FileListRes) processCh() {
for {
select {
case p, ok := <-s.ch:
if ok {
s.List = append(s.List, p)
s.Map[p] = struct{}{}
} else {
s.over <- struct{}{}
}
}
}
}
func GetFileList(dir string, opts ...Option) *FileListRes {
op := &option{}
for _, o := range opts {
o(op)
}
res := &FileListRes{
Map: make(map[string]struct{}),
List: make([]string, 0),
ch: make(chan string, 512),
over: make(chan struct{}),
}
res.option = op
go res.processCh()
t := NewDirTraverse(dir, res.callbackForGetFileList)
t.Handle(opts...)
t.WorkSheet.Wait()
close(res.ch)
<-res.over
return res
}