🚧 WalkFiles

This commit is contained in:
Dan Jones 2024-10-23 21:24:10 -05:00
commit 19206a9bf5
6 changed files with 157 additions and 16 deletions

63
convids/logic.go Normal file
View file

@ -0,0 +1,63 @@
package convids
import (
"os"
fp "path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
func NewData(path string) (*Data, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
ydec := yaml.NewDecoder(f)
var data Data
err = ydec.Decode(&data)
return &data, err
}
func ensureExtRe(c *Config) (err error) {
if c.extRe != nil {
return nil
}
c.extRe, err = regexp.Compile("(" + strings.Join(c.Extensions, "|") + ")$")
return
}
func WalkFiles(d *Data, stopOnError, silent bool, cb func(s *Show, path string) error) (err error) {
err = ensureExtRe(d.Config)
if err != nil {
return
}
var files []os.DirEntry
files, err = os.ReadDir(d.Config.Source)
if err != nil {
return
}
for s := range d.AllShows(silent) {
var found bool
for _, file := range files {
if file.IsDir() {
continue
}
if !d.Config.extRe.MatchString(file.Name()) {
continue
}
found, err = s.Match(file.Name())
if err != nil {
return
}
if found {
err = cb(s, fp.Join(d.Config.Source, file.Name()))
if err != nil && stopOnError {
return
}
}
}
}
return nil
}