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 ErrNoFiles = errors.New("no files processed")
var ErrSkipped = errors.New("skipped") 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() { if file.IsDir() {
return return
} }
if !conf.extRe.MatchString(file.Name()) { if !input.extRe.MatchString(file.Name()) {
return return
} }
show := input.show
var found bool var found bool
found, processError = show.Match(file.Name()) found, processError = show.Match(file.Name())
if processError != nil || !found { if processError != nil || !found {
return return
} }
path := fp.Join(conf.Source, file.Name()) path := fp.Join(input.source, file.Name())
for _, ext := range conf.Skip { for _, ext := range input.skips {
if !strings.HasPrefix(ext, ".") { if !strings.HasPrefix(ext, ".") {
ext = "." + 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 { if cbError == nil {
processed = true processed = true
} }
return 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) { func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) {
err = ensureExtRe(d.Config) err = ensureExtRe(d.Config)
if err != nil { if err != nil {
@ -80,14 +109,21 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e
cb = DryRun(os.Stdout) cb = DryRun(os.Stdout)
} }
count := 0 count := 0
allFiles := fileMap{}
var files []os.DirEntry var files []os.DirEntry
files, err = os.ReadDir(d.Config.Source)
if err != nil { if err != nil {
return return
} }
for s := range d.AllShows(gp) { for s := range d.AllShows(gp) {
if len(s.Sources) == 0 {
s.Sources = []string{d.Config.Source}
}
for _, source := range s.Sources {
files = allFiles.GetFiles(source)
for _, file := range files { for _, file := range files {
processed, processErr, cbErr := processFile(d.Config, cb, s, file) pIn := processInput{file, source, s, d.Config.extRe, d.Config.Skip, cb}
processed, processErr, cbErr := processFile(pIn)
if !processed && processErr == nil && cbErr == nil { if !processed && processErr == nil && cbErr == nil {
continue continue
} }
@ -109,6 +145,7 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e
break break
} }
} }
}
if err == nil && count < 1 { if err == nil && count < 1 {
fmt.Println("Found nothing") fmt.Println("Found nothing")
return ErrNoFiles return ErrNoFiles

View file

@ -25,6 +25,7 @@ type Show struct {
Pattern string Pattern string
Name string Name string
Anime bool Anime bool
Sources []string
re *regexp.Regexp re *regexp.Regexp
} }