choose what to arrange: -kinds, -exclude, am survey

-kinds picks any of image, raw, video (default all). -exclude skips
files and whole directories by name glob, and can be repeated. am
survey stats media per directory (counts by kind, videos over 1G,
sizes) so things like ripped DVDs stand out before arranging. It
never opens files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sm
2026-10-26 15:57:23 -07:00
co-authored by Claude Opus 5.5
parent 6d971118b7
commit f63b855179
6 changed files with 270 additions and 15 deletions
+61 -3
View File
@@ -7,12 +7,15 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"mcquay.me/arrange"
)
const usage = "am <arr|clean|verify|meta> [flags]"
const arrUsage = "am arr [-h|-n|-hash=sha256|md5|-cores=N] <in> <out>"
const usage = "am <arr|survey|clean|verify|meta> [flags]"
const arrUsage = "am arr [-h|-n|-kinds=image,raw,video|-exclude=PATTERN...|-hash=sha256|md5|-cores=N] <in> <out>"
const surveyUsage = "am survey [-h|-depth=N|-kinds=...|-exclude=PATTERN...] <directory>"
const cleanUsage = "am clean [-h|-n|-cores=N] <directory>"
const verifyUsage = "am verify [-h|-cores=N] <directory>"
const metaUsage = "am meta [-h|-cores=N] <file0> <file1> ... <fileN>"
@@ -20,6 +23,45 @@ const metaUsage = "am meta [-h|-cores=N] <file0> <file1> ... <fileN>"
var cores = flag.Int("cores", 0, "how many threads to use")
var dryRun = flag.Bool("n", false, "dry run: print the plan as tab-separated lines, change nothing")
var hashName = flag.String("hash", "sha256", "content-address hash: sha256, or md5 to add to a store built by older versions")
var kinds = flag.String("kinds", "image,raw,video", "comma-separated kinds of media to include: image, raw, video")
var depth = flag.Int("depth", 1, "survey: how many directory levels to group by")
var excludes patterns
func init() {
flag.Var(&excludes, "exclude", "skip files and directories whose name matches this glob (repeatable), e.g. VIDEO_TS or '*.vob'")
}
// patterns collects a repeated flag.
type patterns []string
func (p *patterns) String() string { return strings.Join(*p, ",") }
func (p *patterns) Set(v string) error {
if _, err := filepath.Match(v, ""); err != nil {
return fmt.Errorf("bad pattern %q: %v", v, err)
}
*p = append(*p, v)
return nil
}
// filter builds the arrange.Filter from the -kinds and -exclude flags.
func filter() (arrange.Filter, error) {
f := arrange.Filter{Classes: map[arrange.Class]bool{}, Exclude: excludes}
for _, k := range strings.Split(*kinds, ",") {
c := arrange.Class(strings.TrimSpace(k))
switch c {
case arrange.Image, arrange.Raw, arrange.Video:
f.Classes[c] = true
case "":
default:
return f, fmt.Errorf("unknown kind %q (known: image, raw, video)", k)
}
}
if len(f.Classes) == 0 {
return f, fmt.Errorf("-kinds selects nothing")
}
return f, nil
}
func main() {
if len(os.Args) < 2 {
@@ -42,6 +84,12 @@ func main() {
os.Exit(1)
}
f, err := filter()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
switch sub {
case "ar", "arr", "arrange":
args := flag.Args()
@@ -50,10 +98,20 @@ func main() {
os.Exit(1)
}
in, out := args[0], args[1]
if err := arr(in, out); err != nil {
if err := arr(in, out, f); err != nil {
fmt.Fprintf(os.Stderr, "problem arranging media: %v\n", err)
os.Exit(1)
}
case "s", "survey":
args := flag.Args()
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "%s\n", surveyUsage)
os.Exit(1)
}
if err := survey(args[0], f); err != nil {
fmt.Fprintf(os.Stderr, "problem surveying: %v\n", err)
os.Exit(1)
}
case "c", "cl", "clean":
args := flag.Args()
if len(args) != 1 {