accept mv n ln
parent
805e666230
commit
dc8c9fdf0c
17
main.go
17
main.go
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,9 +64,9 @@ func TestRun(t *testing.T) {
|
|||
}
|
||||
outd := t.TempDir()
|
||||
|
||||
if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides); err != nil {
|
||||
if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides, main.RealMvNLn); err != nil {
|
||||
t.Fatal("err on first run:", err)
|
||||
} else if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides); err != nil {
|
||||
} else if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides, main.RealMvNLn); err != nil {
|
||||
t.Fatal("err on second run:", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue