generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
54 lines (43 loc) · 968 Bytes
/
scanner.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
package main
import (
"bytes"
"io/fs"
"github.com/xmidt-org/thoth"
)
type Scanner struct {
Root fs.FS
Selector thoth.Selector
Logger Logger
}
func (s Scanner) Scan() ([]thoth.Template, Samples, error) {
var (
buffer = bytes.NewBuffer(make([]byte, 0, 1024))
samples Samples
templates []thoth.Template
)
err := fs.WalkDir(s.Root, ".", func(path string, entry fs.DirEntry, walkErr error) error {
if walkErr == nil && !entry.IsDir() {
if p, found := s.Selector.Select(path); found {
var t thoth.Template
f, err := s.Root.Open(path)
if err == nil {
defer f.Close()
buffer.Reset()
_, err = buffer.ReadFrom(f)
}
if err == nil {
t, err = p.Parse(path, buffer.String())
}
s.Logger.Result(TemplateResult{
Name: path,
Err: err,
})
if err == nil {
templates = append(templates, t)
}
}
}
return nil // always continue
})
return templates, samples, err
}