-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewalker.go
44 lines (39 loc) · 923 Bytes
/
filewalker.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
package main
import (
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
func Scan(output chan string, input ...string) error {
defer close(output)
for _, elem := range input {
stat, err := os.Stat(elem)
if err != nil {
return errors.Wrapf(err, "could not stat '%s'", elem)
}
if !stat.IsDir() && strings.HasSuffix(elem, ".go") {
output <- elem
continue
}
err = filepath.WalkDir(elem, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return errors.Wrapf(err, "could not walk '%s'", path)
}
if !info.IsDir() && strings.HasSuffix(path, ".go") {
output <- path
return nil
}
// skip private dirs like .git
if info.IsDir() && len(info.Name()) > 1 && strings.HasPrefix(info.Name(), ".") {
return fs.SkipDir
}
return nil
})
if err != nil {
return errors.Wrapf(err, "could not walk '%s'", elem)
}
}
return nil
}