diff --git a/cmd/recordio/recordio.go b/cmd/recordio/recordio.go new file mode 100644 index 0000000..41f543d --- /dev/null +++ b/cmd/recordio/recordio.go @@ -0,0 +1,70 @@ +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) + } +}