From 805e666230c7c2a02a0faae42930205d671de2a2 Mon Sep 17 00:00:00 2001 From: bel Date: Fri, 4 Apr 2025 22:28:38 -0600 Subject: [PATCH] args are OUTD IND {} patterns.. --- main.go | 52 ++++++++++++++++++++++++++++++++++++---------------- main_test.go | 30 ++++++++++-------------------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/main.go b/main.go index c77b6b4..24a9ba4 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( "context" + "encoding/json" "fmt" - "maps" "os" "os/signal" "path" @@ -12,26 +12,38 @@ import ( "syscall" ) +type Fields struct { + Title string + Season string + Episode string +} + func main() { ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT) defer can() + var overrides Fields + json.Unmarshal([]byte(os.Args[3]), &overrides) + if err := Run(ctx, os.Args[1], os.Args[2], - os.Args[3:], - nil, + os.Args[4:], + overrides, ); err != nil { panic(err) } } -func Run(ctx context.Context, outd, ind string, patterns []string, overrides map[string]string) error { +func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields) error { entries, err := os.ReadDir(ind) if err != nil { return err } for _, entry := range entries { + if !entry.Type().IsRegular() { + continue + } if err := one(ctx, outd, path.Join(ind, entry.Name()), patterns, overrides); err != nil { return err } @@ -39,7 +51,7 @@ func Run(ctx context.Context, outd, ind string, patterns []string, overrides map return nil } -func one(ctx context.Context, outd, inf string, patterns []string, overrides map[string]string) error { +func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields) error { f := path.Base(inf) for _, pattern := range patterns { re := regexp.MustCompile(pattern) @@ -47,30 +59,38 @@ func one(ctx context.Context, outd, inf string, patterns []string, overrides map continue } - found := maps.Clone(overrides) + found := overrides groupNames := re.SubexpNames() groups := re.FindStringSubmatch(f) for i := 1; i < len(groupNames); i++ { - k := groupNames[i] v := groups[i] - found[k] = v + switch groupNames[i] { + case "title": + found.Title = v + case "season": + found.Season = v + case "episode": + found.Episode = v + default: + return fmt.Errorf("unexpected capture group %q", groupNames[i]) + } } - if found["title"] == "" || found["season"] == "" || found["episode"] == "" { + if found.Title == "" || found.Season == "" || found.Episode == "" { continue } - found["title"] = strings.Join(strings.Fields(found["title"]), "_") + found.Title = strings.Join(strings.Fields(found.Title), "_") - if err := mvNLn(ctx, outd, inf, found["title"], found["season"], found["episode"]); err != nil { - return err - } - return nil + return mvNLn(ctx, outd, inf, found) } return nil } -func mvNLn(ctx context.Context, outd, inf string, title, season, episode string) error { - outf := path.Join(outd, fmt.Sprintf("%s_S%sE%s%s", title, season, episode, path.Ext(inf))) +func mvNLn(ctx context.Context, outd, inf string, fields Fields) error { + outf := path.Join(outd, fmt.Sprintf("%s_S%sE%s%s", fields.Title, fields.Season, fields.Episode, path.Ext(inf))) + if _, err := os.Stat(outf); err == nil { + return nil // fmt.Errorf("conflict: %s already exists", path.Base(outf)) + } if err := os.Rename(inf, outf); err != nil { return err } diff --git a/main_test.go b/main_test.go index e2909d9..c56c341 100644 --- a/main_test.go +++ b/main_test.go @@ -15,7 +15,7 @@ func TestRun(t *testing.T) { cases := map[string]struct { given []string patterns []string - overrides map[string]string + overrides main.Fields want []string }{ "empty": {}, @@ -29,8 +29,8 @@ func TestRun(t *testing.T) { ".urvivor.[Aa][Uu].*[sS](?P[0-9]+)[eE](?P[0-9]*).*1080.*MeGusta", ".urvivor.[Aa][Uu].*[sS](?P[0-9]+)[eE](?P[0-9]*).*720.*MeGusta", }, - overrides: map[string]string{ - "title": "Australian_Survivor", + overrides: main.Fields{ + Title: "Australian_Survivor", }, want: []string{ "Australian_Survivor_S12E11.mkv", @@ -45,8 +45,8 @@ func TestRun(t *testing.T) { patterns: []string{ `^\[[^\]]*\] (?P.*) - (?<episode>[0-9]*)`, }, - overrides: map[string]string{ - "season": "01", + overrides: main.Fields{ + Season: "01", }, want: []string{ "Tokidoki_Bosotto_Russia-go_de_Dereru_Tonari_no_Alya-san_S01E01.mkv", @@ -65,7 +65,9 @@ func TestRun(t *testing.T) { outd := t.TempDir() if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides); err != nil { - t.Fatal(err) + t.Fatal("err on first run:", err) + } else if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides); err != nil { + t.Fatal("err on second run:", err) } for _, f := range c.want { @@ -84,20 +86,8 @@ func TestRun(t *testing.T) { if !slices.Contains(c.want, path.Base(entry.Name())) { t.Errorf("unexpected %s", entry.Name()) } - if stat, err := entry.Info(); err != nil { - t.Errorf("cant read info of %s: %v", entry.Name(), err) - } else if !stat.Mode().IsRegular() { - t.Errorf("non-regular file %s in out: %v", entry.Name(), stat.Mode()) - } - } - } - - if entries, err := os.ReadDir(outd); err != nil { - t.Error("failed to list outdir: %w", err) - } else { - for _, entry := range entries { - if !slices.Contains(c.want, path.Base(entry.Name())) { - t.Errorf("unexpected %s", entry.Name()) + if !entry.Type().IsRegular() { + t.Errorf("non-regular file %s in out: %v", entry.Name(), entry.Type()) } } }