35 lines
649 B
Go
35 lines
649 B
Go
package util
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"path/filepath"
|
|
)
|
|
|
|
type ParseFunc func(data []byte) int
|
|
|
|
func ParseFiles(dir string, parser ParseFunc) {
|
|
files, err := ioutil.ReadDir(dir)
|
|
if err != nil {
|
|
log.Print(err.Error())
|
|
return
|
|
}
|
|
|
|
for _, file := range files {
|
|
if filepath.Ext(file.Name()) == ".yml" || filepath.Ext(file.Name()) == ".yaml" {
|
|
ParseFile(dir+file.Name(), parser)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ParseFile(fileName string, parser ParseFunc) {
|
|
data, err := ioutil.ReadFile(fileName)
|
|
if err != nil {
|
|
log.Print(err.Error())
|
|
return
|
|
}
|
|
|
|
count := parser(data)
|
|
log.Printf("util.parseFile: Added %d new entries from %s", count, fileName)
|
|
}
|