♻️ Better names and add printing methods

This commit is contained in:
Dan Jones 2024-11-04 15:20:09 -06:00
commit d5e7f601b6
4 changed files with 41 additions and 17 deletions

View file

@ -14,6 +14,7 @@ const dataPath = "shows.yml"
func main() { func main() {
loop := pflag.BoolP("loop", "l", false, "Loop") loop := pflag.BoolP("loop", "l", false, "Loop")
help := pflag.BoolP("help", "h", false, "Get Help") help := pflag.BoolP("help", "h", false, "Get Help")
dryRun := pflag.BoolP("dry-run", "d", false, "Do a dry run")
pflag.Parse() pflag.Parse()
if *help { if *help {
@ -21,6 +22,12 @@ func main() {
os.Exit(2) os.Exit(2)
} }
*dryRun = true // @todo temp
var walker convids.ShowWalker
if *dryRun {
walker = convids.DryRun(os.Stdout)
}
if *loop { if *loop {
fmt.Println("looping") fmt.Println("looping")
} }
@ -28,9 +35,6 @@ func main() {
data, err := convids.NewData(dataPath) data, err := convids.NewData(dataPath)
e.HandleErr(err) e.HandleErr(err)
convids.WalkFiles(data, false, false, func(s *convids.Show, path string) error { convids.WalkFiles(data, false, convids.PrintGroupName(os.Stdout), walker)
fmt.Printf("%s for %+v\n", path, s)
return nil
})
fmt.Println("Done!") fmt.Println("Done!")
} }

View file

@ -1,6 +1,8 @@
package convids package convids
import ( import (
"fmt"
"io"
"os" "os"
fp "path/filepath" fp "path/filepath"
"regexp" "regexp"
@ -28,17 +30,23 @@ func ensureExtRe(c *Config) (err error) {
return return
} }
func WalkFiles(d *Data, stopOnError, silent bool, cb func(s *Show, path string) error) (err error) { type ShowWalker func(show *Show, path string) error
type GroupPrinter func(name string, group Shows)
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 {
return return
} }
if cb == nil {
cb = DryRun(os.Stdout)
}
var files []os.DirEntry var files []os.DirEntry
files, err = os.ReadDir(d.Config.Source) files, err = os.ReadDir(d.Config.Source)
if err != nil { if err != nil {
return return
} }
for s := range d.AllShows(silent) { for s := range d.AllShows(gp) {
var found bool var found bool
for _, file := range files { for _, file := range files {
if file.IsDir() { if file.IsDir() {
@ -62,3 +70,16 @@ func WalkFiles(d *Data, stopOnError, silent bool, cb func(s *Show, path string)
} }
return nil return nil
} }
func DryRun(out io.Writer) ShowWalker {
return func(s *Show, path string) error {
fmt.Fprintf(out, "Saving %s to %s\n", path, s.Folder)
return nil
}
}
func PrintGroupName(out io.Writer) GroupPrinter {
return func(name string, group Shows) {
fmt.Fprintf(out, "Checking %s shows\n\n", name)
}
}

View file

@ -2,7 +2,6 @@ package convids
import ( import (
"errors" "errors"
"fmt"
"iter" "iter"
"regexp" "regexp"
"slices" "slices"
@ -57,24 +56,24 @@ func (ss *Shows) All() iter.Seq[*Show] {
return slices.Values(*ss) return slices.Values(*ss)
} }
func (d *Data) AllShows(silent bool) iter.Seq[*Show] { func (d *Data) AllShows(outputGroup GroupPrinter) iter.Seq[*Show] {
return func(yield func(*Show) bool) { return func(yield func(*Show) bool) {
if d == nil || d.Config == nil { if d == nil || d.Config == nil {
return return
} }
for _, show := range d.Config.Shows { for _, groupName := range d.Config.Groups {
sh := (*d.Shows)[show] group := (*d.Shows)[groupName]
if sh == nil || len(*sh) < 1 { if group == nil || len(*group) < 1 {
continue continue
} }
if !silent { if outputGroup != nil {
fmt.Printf("Checking %s shows\n\n", show) outputGroup(groupName, *group)
} }
for s := range sh.All() { for sh := range group.All() {
if s == nil { if sh == nil {
continue continue
} }
if !yield(s) { if !yield(sh) {
return return
} }
} }

View file

@ -10,7 +10,7 @@ type Data struct {
type Config struct { type Config struct {
Source string Source string
Extensions []string Extensions []string
Shows []string Groups []string
extRe *regexp.Regexp extRe *regexp.Regexp
} }