urizen/lib/util/file.go

35 lines
649 B
Go
Raw Normal View History

2020-07-16 06:24:19 +00:00
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)
}