Initial commit.
This commit is contained in:
34
lib/util/file.go
Normal file
34
lib/util/file.go
Normal file
@ -0,0 +1,34 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user