add subcommands, make way for clean

This commit is contained in:
Stephen McQuay 2016-07-09 15:06:51 -07:00
parent c2c01c46f1
commit ceb4206787
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
2 changed files with 69 additions and 40 deletions

47
cmd/am/arrange.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"fmt"
"log"
"runtime"
"mcquay.me/arrange"
)
func arr(indir, outdir string) error {
if err := arrange.PrepOutput(outdir); err != nil {
return fmt.Errorf("problem creating directory structure: %v", err)
}
work := arrange.Source(indir)
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 := stats{}
for err := range arrange.Move(arrange.Merge(streams), outdir) {
st.total++
if err != nil {
switch err.(type) {
case arrange.Dup:
st.dupes++
default:
log.Printf("%+v", err)
}
} else {
st.moved++
}
}
log.Printf("dupes: %+v", st.dupes)
log.Printf("moved: %+v", st.moved)
log.Printf("total: %+v", st.total)
return nil
}

View File

@ -5,12 +5,10 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime"
"mcquay.me/arrange"
) )
const usage = "aj <indir> <outdir>" const usage = "am <arr|help> [flags]"
const arrUsage = "am arr [-h|-cores=N] <in> <out>"
type stats struct { type stats struct {
total int total int
@ -21,47 +19,31 @@ type stats struct {
var cores = flag.Int("cores", 0, "how many threads to use") var cores = flag.Int("cores", 0, "how many threads to use")
func main() { func main() {
flag.Parse() if len(os.Args) < 2 {
log.SetFlags(log.Lshortfile)
if len(flag.Args()) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", usage) fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1) os.Exit(1)
} }
in, out := flag.Args()[0], flag.Args()[1] var sub string
sub, os.Args = os.Args[1], os.Args[1:]
if err := arrange.PrepOutput(out); err != nil { flag.Parse()
fmt.Fprintf(os.Stderr, "problem creating directory structure: %v", err) log.SetFlags(log.Lshortfile)
switch sub {
case "ar", "arr", "arrange":
args := flag.Args()
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", arrUsage)
os.Exit(1)
}
in, out := args[0], args[1]
if err := arr(in, out); err != nil {
fmt.Fprintf(os.Stderr, "problem arranging media: %v\n", err)
os.Exit(1)
}
default:
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1) os.Exit(1)
} }
work := arrange.Source(in)
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 := stats{}
for err := range arrange.Move(arrange.Merge(streams), out) {
st.total++
if err != nil {
switch err.(type) {
case arrange.Dup:
st.dupes++
default:
log.Printf("%+v", err)
}
} else {
st.moved++
}
}
log.Printf("dupes: %+v", st.dupes)
log.Printf("moved: %+v", st.moved)
log.Printf("total: %+v", st.total)
} }