From 1b072f42d358ddab93e8e5cc95bb4ecbda37c116 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 16 Nov 2024 23:38:44 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20alternate=20source=20per?= =?UTF-8?q?=20show?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convids/logic.go | 89 +++++++++++++++++++++++++++++++++-------------- convids/models.go | 1 + 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/convids/logic.go b/convids/logic.go index 80ef641..63fe2bc 100644 --- a/convids/logic.go +++ b/convids/logic.go @@ -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 { diff --git a/convids/models.go b/convids/models.go index b7ad695..58b8992 100644 --- a/convids/models.go +++ b/convids/models.go @@ -25,6 +25,7 @@ type Show struct { Pattern string Name string Anime bool + Sources []string re *regexp.Regexp }