-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.go
50 lines (42 loc) · 903 Bytes
/
extractor.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
package snippet
import (
"github.com/pkg/errors"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func extractSnippetsFromDir(root string) (snippets, error) {
ss := snippets{}
walkfunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
return nil
}
bt, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("skipping %s: %s", path, err)
return nil
}
ext := filepath.Ext(info.Name())
p, err := newParser(ext)
if err != nil {
log.Printf("skipping %s: %s", path, err)
return nil
}
s, err := snippetsFromContent(p, string(bt))
if err != nil {
log.Printf("skipping %s: %s", path, err)
return nil
}
ss.addSnippets(s)
return nil
}
err := filepath.Walk(root, walkfunc)
if err != nil {
return snippets{}, errors.Wrapf(err, "failed extracting snippets from %v", root)
}
return ss, nil
}