accept mv n ln
parent
805e666230
commit
dc8c9fdf0c
17
main.go
17
main.go
|
|
@ -18,6 +18,8 @@ type Fields struct {
|
||||||
Episode string
|
Episode string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MvNLn func(string, string) error
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||||
defer can()
|
defer can()
|
||||||
|
|
@ -30,12 +32,13 @@ func main() {
|
||||||
os.Args[2],
|
os.Args[2],
|
||||||
os.Args[4:],
|
os.Args[4:],
|
||||||
overrides,
|
overrides,
|
||||||
|
RealMvNLn,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
panic(err)
|
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)
|
entries, err := os.ReadDir(ind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -44,14 +47,14 @@ func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fie
|
||||||
if !entry.Type().IsRegular() {
|
if !entry.Type().IsRegular() {
|
||||||
continue
|
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 err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
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)
|
f := path.Base(inf)
|
||||||
for _, pattern := range patterns {
|
for _, pattern := range patterns {
|
||||||
re := regexp.MustCompile(pattern)
|
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), "_")
|
found.Title = strings.Join(strings.Fields(found.Title), "_")
|
||||||
|
|
||||||
return mvNLn(ctx, outd, inf, found)
|
return foundOne(ctx, outd, inf, found, mvNLn)
|
||||||
}
|
}
|
||||||
return nil
|
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)))
|
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 {
|
if _, err := os.Stat(outf); err == nil {
|
||||||
return nil // fmt.Errorf("conflict: %s already exists", path.Base(outf))
|
return nil // fmt.Errorf("conflict: %s already exists", path.Base(outf))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,9 +64,9 @@ func TestRun(t *testing.T) {
|
||||||
}
|
}
|
||||||
outd := t.TempDir()
|
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)
|
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)
|
t.Fatal("err on second run:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue