Add never really used code to build a recordio file from multiple emails.
This commit is contained in:
parent
5a04c7cc28
commit
9fa0d7ad59
70
cmd/recordio/recordio.go
Normal file
70
cmd/recordio/recordio.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user