Support alternate source per show

This commit is contained in:
Dan Jones 2024-11-16 23:38:44 -06:00
commit 1b072f42d3
2 changed files with 64 additions and 26 deletions

View file

@ -39,21 +39,32 @@ type GroupPrinter func(name string, group Shows)
var ErrNoFiles = errors.New("no files processed")
var ErrSkipped = errors.New("skipped")
func processFile(conf *Config, walk ShowWalker, show *Show, file os.DirEntry) (processed bool, processError, cbError error) {
type processInput struct {
file os.DirEntry
source string
show *Show
extRe *regexp.Regexp
skips []string
walk ShowWalker
}
func processFile(input processInput) (processed bool, processError, cbError error) {
file := input.file
if file.IsDir() {
return
}
if !conf.extRe.MatchString(file.Name()) {
if !input.extRe.MatchString(file.Name()) {
return
}
show := input.show
var found bool
found, processError = show.Match(file.Name())
if processError != nil || !found {
return
}
path := fp.Join(conf.Source, file.Name())
for _, ext := range conf.Skip {
path := fp.Join(input.source, file.Name())
for _, ext := range input.skips {
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
@ -64,13 +75,31 @@ func processFile(conf *Config, walk ShowWalker, show *Show, file os.DirEntry) (p
}
}
cbError = walk(show, path)
cbError = input.walk(show, path)
if cbError == nil {
processed = true
}
return
}
type fileMap map[string][]os.DirEntry
func (fm fileMap) GetFiles(path string) []os.DirEntry {
files, ok := fm[path]
if ok {
return files
}
fm.ReadPath(path)
return fm[path]
}
func (fm fileMap) ReadPath(path string) {
if _, ok := fm[path]; ok {
return
}
fm[path], _ = os.ReadDir(path)
}
func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) {
err = ensureExtRe(d.Config)
if err != nil {
@ -80,33 +109,41 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e
cb = DryRun(os.Stdout)
}
count := 0
allFiles := fileMap{}
var files []os.DirEntry
files, err = os.ReadDir(d.Config.Source)
if err != nil {
return
}
for s := range d.AllShows(gp) {
for _, file := range files {
processed, processErr, cbErr := processFile(d.Config, cb, s, file)
if !processed && processErr == nil && cbErr == nil {
continue
if len(s.Sources) == 0 {
s.Sources = []string{d.Config.Source}
}
for _, source := range s.Sources {
files = allFiles.GetFiles(source)
for _, file := range files {
pIn := processInput{file, source, s, d.Config.extRe, d.Config.Skip, cb}
processed, processErr, cbErr := processFile(pIn)
if !processed && processErr == nil && cbErr == nil {
continue
}
if errors.Is(processErr, ErrSkipped) {
continue
}
if processErr != nil {
return processErr
}
if cbErr != nil && stopOnError {
return cbErr
}
if cbErr == nil && processed {
count++
}
if cbErr != nil {
err = cbErr
}
break
}
if errors.Is(processErr, ErrSkipped) {
continue
}
if processErr != nil {
return processErr
}
if cbErr != nil && stopOnError {
return cbErr
}
if cbErr == nil && processed {
count++
}
if cbErr != nil {
err = cbErr
}
break
}
}
if err == nil && count < 1 {