test recursive and run as go run ./ r
This commit is contained in:
112
main.go
112
main.go
@@ -5,13 +5,15 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Fields struct {
|
||||
@@ -26,19 +28,65 @@ func main() {
|
||||
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||
defer can()
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
if err := Recursive(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
if err := Main(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
foo := Main
|
||||
if len(os.Args) == 2 && os.Args[1] == "r" {
|
||||
foo = Recursive
|
||||
}
|
||||
if err := foo(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Recursive(ctx context.Context) error {
|
||||
return io.EOF
|
||||
q := []string{"./"}
|
||||
for len(q) > 0 {
|
||||
d := q[0]
|
||||
q = q[1:]
|
||||
|
||||
p := path.Join(d, ".show-ingestion.yaml")
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
} else if err := func() error {
|
||||
var y struct {
|
||||
C Fields
|
||||
O string
|
||||
D bool
|
||||
P []string
|
||||
}
|
||||
b, _ := os.ReadFile(path.Join(d, ".show-ingestion.yaml"))
|
||||
if err := yaml.Unmarshal(b, &y); err != nil {
|
||||
return fmt.Errorf("%s: %w", p, err)
|
||||
}
|
||||
|
||||
was, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Chdir(d); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Chdir(was)
|
||||
|
||||
if err := Run(ctx, y.O, "./", y.P, y.C, y.D); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Chdir(was)
|
||||
}(); err != nil {
|
||||
return fmt.Errorf("%s: %w", p, err)
|
||||
}
|
||||
|
||||
entries, err := readDir(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
q = append(q, path.Join(d, entry.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Main(ctx context.Context) error {
|
||||
@@ -54,24 +102,34 @@ func Main(ctx context.Context) error {
|
||||
var overrides Fields
|
||||
json.Unmarshal([]byte(*overridesS), &overrides)
|
||||
|
||||
mvNLn := RealMvNLn
|
||||
if *dry {
|
||||
mvNLn = DryMvNLn()
|
||||
}
|
||||
|
||||
return Run(ctx,
|
||||
*outd,
|
||||
*ind,
|
||||
append(flags.Args(),
|
||||
`^\[[^\]]*\] (?P<title>.*) - (?P<episode>[0-9]+) .*`,
|
||||
flags.Args(),
|
||||
overrides,
|
||||
*dry,
|
||||
)
|
||||
}
|
||||
|
||||
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields, dry bool) error {
|
||||
mvNLn := RealMvNLn
|
||||
if dry {
|
||||
mvNLn = DryMvNLn()
|
||||
}
|
||||
return RunWith(ctx,
|
||||
outd,
|
||||
ind,
|
||||
append(patterns,
|
||||
`^\[[^\]]*\] (?P<title>.*) - (?P<episode>[0-9]+).*`,
|
||||
`^(?P<title>.*) S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*`,
|
||||
),
|
||||
overrides,
|
||||
mvNLn,
|
||||
)
|
||||
}
|
||||
|
||||
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields, mvNLn MvNLn) error {
|
||||
entries, err := os.ReadDir(ind)
|
||||
func RunWith(ctx context.Context, outd, ind string, patterns []string, overrides Fields, mvNLn MvNLn) error {
|
||||
entries, err := readDir(ind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -131,7 +189,10 @@ func RealMvNLn(outf, inf string) error {
|
||||
return fmt.Errorf("cannot mv_n_ln(%s): (%v) mode=%v", inf, err, stat.Mode())
|
||||
}
|
||||
if _, err := os.Stat(outf); err == nil {
|
||||
return nil // fmt.Errorf("conflict: %s already exists", path.Base(outf))
|
||||
return nil
|
||||
}
|
||||
if err := os.MkdirAll(path.Dir(outf), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(inf, outf); err != nil {
|
||||
return err
|
||||
@@ -155,3 +216,14 @@ func DryMvNLn() func(string, string) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func readDir(d string) ([]fs.DirEntry, error) {
|
||||
entries, err := os.ReadDir(d)
|
||||
result := []fs.DirEntry{}
|
||||
for _, entry := range entries {
|
||||
if !strings.HasPrefix(entry.Name(), ".") {
|
||||
result = append(result, entry)
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user