accept mv n ln

This commit is contained in:
bel
2025-04-04 22:59:50 -06:00
parent 805e666230
commit dc8c9fdf0c
2 changed files with 14 additions and 7 deletions

17
main.go
View File

@@ -18,6 +18,8 @@ type Fields struct {
Episode string
}
type MvNLn func(string, string) error
func main() {
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer can()
@@ -30,12 +32,13 @@ func main() {
os.Args[2],
os.Args[4:],
overrides,
RealMvNLn,
); err != nil {
panic(err)
}
}
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields) error {
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields, mvNLn MvNLn) error {
entries, err := os.ReadDir(ind)
if err != nil {
return err
@@ -44,14 +47,14 @@ func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fie
if !entry.Type().IsRegular() {
continue
}
if err := one(ctx, outd, path.Join(ind, entry.Name()), patterns, overrides); err != nil {
if err := one(ctx, outd, path.Join(ind, entry.Name()), patterns, overrides, mvNLn); err != nil {
return err
}
}
return nil
}
func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields) error {
func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields, mvNLn MvNLn) error {
f := path.Base(inf)
for _, pattern := range patterns {
re := regexp.MustCompile(pattern)
@@ -81,13 +84,17 @@ func one(ctx context.Context, outd, inf string, patterns []string, overrides Fie
}
found.Title = strings.Join(strings.Fields(found.Title), "_")
return mvNLn(ctx, outd, inf, found)
return foundOne(ctx, outd, inf, found, mvNLn)
}
return nil
}
func mvNLn(ctx context.Context, outd, inf string, fields Fields) error {
func foundOne(ctx context.Context, outd, inf string, fields Fields, mvNLn MvNLn) error {
outf := path.Join(outd, fmt.Sprintf("%s_S%sE%s%s", fields.Title, fields.Season, fields.Episode, path.Ext(inf)))
return mvNLn(outf, inf)
}
func RealMvNLn(outf, inf string) error {
if _, err := os.Stat(outf); err == nil {
return nil // fmt.Errorf("conflict: %s already exists", path.Base(outf))
}