Files
arrange/cmd/am/arrange.go
T

70 lines
1.3 KiB
Go
Raw Normal View History

2016-07-09 15:06:51 -07:00
package main
import (
"fmt"
"log"
"os"
"path/filepath"
2016-07-09 15:06:51 -07:00
"runtime"
"mcquay.me/arrange"
)
func arr(indir, outdir string, f arrange.Filter) error {
if !*dryRun {
if err := arrange.PrepOutput(outdir); err != nil {
return fmt.Errorf("problem creating directory structure: %v", err)
}
2016-07-09 15:06:51 -07:00
}
work := arrange.SourceFiltered(indir, f)
2016-07-09 15:06:51 -07:00
streams := []<-chan arrange.Media{}
workers := runtime.NumCPU()
if *cores != 0 {
workers = *cores
}
for w := 0; w < workers; w++ {
streams = append(streams, arrange.Parse(work))
}
st := newStats()
// in a dry run nothing lands in the store, so remember what would have
seen := map[string]bool{}
for m := range arrange.Merge(streams) {
if *dryRun {
content := m.Content(outdir)
_, err := os.Stat(content)
dup := seen[m.Hash] || err == nil
seen[m.Hash] = true
action := "new"
if dup {
action = "dup"
2016-07-09 15:06:51 -07:00
}
date := filepath.Join(outdir, "date", fmt.Sprintf("%04d", m.Time.Year()), fmt.Sprintf("%02d", m.Time.Month()))
fmt.Printf("%s\t%s\t%s\t%s\n", action, m.Path, content, date)
st.add(m, dup)
continue
}
err := m.Move(outdir)
switch err.(type) {
case nil:
st.add(m, false)
case arrange.Dup:
st.add(m, true)
default:
st.errors++
log.Printf("%+v", err)
2016-07-09 15:06:51 -07:00
}
}
verb := "moved"
if *dryRun {
verb = "would move"
}
st.write(os.Stderr, verb)
2016-07-09 15:06:51 -07:00
return nil
}