71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"flag"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/eclesh/recordio"
|
|
"github.com/golang/glog"
|
|
"xinu.tv/email/maildir"
|
|
)
|
|
|
|
var (
|
|
maildirPath = flag.String("maildir", filepath.Join(os.Getenv("HOME"), "Maildir"), "Maildir path")
|
|
recordIOPath = flag.String("recordio", "/tmp/maildir.rio", "path to recordIO file")
|
|
compress = flag.Bool("z", false, "compress output")
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
defer glog.Flush()
|
|
count := 0
|
|
f, err := os.Create(*recordIOPath)
|
|
if err != nil {
|
|
glog.Exitf("Failed to open %q: %v", *recordIOPath, err)
|
|
}
|
|
var gzf *gzip.Writer
|
|
if *compress {
|
|
gzf, err = gzip.NewWriterLevel(f, gzip.BestCompression)
|
|
if err != nil {
|
|
glog.Exitf("Failed to create gzip writer: %v", err)
|
|
}
|
|
}
|
|
|
|
var w io.Writer
|
|
if *compress {
|
|
w = recordio.NewWriter(gzf)
|
|
} else {
|
|
w = recordio.NewWriter(f)
|
|
}
|
|
if err := maildir.Walk(*maildirPath, func(path string) error {
|
|
b, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := w.Write(b); err != nil {
|
|
return err
|
|
}
|
|
count++
|
|
if count%1000 == 0 {
|
|
glog.Infof("Processed %d files", count)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
glog.Exitf("Failed to walk %q: %v", *maildirPath, err)
|
|
}
|
|
|
|
if *compress {
|
|
if err := gzf.Flush(); err != nil {
|
|
glog.Exitf("Failed to flush gzipper on %q: %v", *recordIOPath, err)
|
|
}
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
glog.Exitf("Failed to close %q: %v", *recordIOPath, err)
|
|
}
|
|
}
|