-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopFiles.go
56 lines (52 loc) · 1.07 KB
/
loopFiles.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
package main
import (
"context"
"log"
"os"
"path/filepath"
)
func loopFiles(ctx context.Context, filePatterns []string, ch <-chan hRequest) <-chan hRequest {
if filePatterns == nil || len(filePatterns) == 0 {
return ch
}
done := ctx.Done()
out := make(chan hRequest)
looper := func(patList []string, c <-chan hRequest) {
defer close(out)
for i := range c {
for _, pattern := range patList {
files, err := filepath.Glob(pattern)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
fi, err := os.Stat(f)
if err != nil {
log.Fatal(err)
}
if !fi.IsDir() {
/*
fullpath, err := filepath.Abs(f)
if err != nil {
log.Print("Warning, can't get full path of ", f, ": ", err)
fullpath = f
}
*/
// log.Print("Found ", fullpath)
data := i
data.Filename = f
//data.Filename = fullpath
data.Size = fi.Size()
select {
case out <- data:
case <-done:
return
}
}
}
}
}
}
go looper(filePatterns, ch)
return out
}