// Given a file containing sorted sha1 hashes in string form, this // writes out a new file containing the same hashes in binary form. package main import ( "bufio" "compress/bzip2" "encoding/hex" "flag" "io" "log" "os" "github.com/dustin/go-humanize" ) var saw int64 func maybefatal(err error) { if err != nil { log.Fatalf("Dying with %v", err) } } func process(r *bufio.Reader, w io.Writer) { for { s, err := r.ReadString(byte('\n')) if err == io.EOF { log.Printf("Got an EOF") return } maybefatal(err) bytes, err := hex.DecodeString(s[:40]) maybefatal(err) n, err := w.Write(bytes) maybefatal(err) if n != 20 { log.Fatalf("Expected to write 20 bytes, wrote %d", n) } saw += 1 } } func main() { flag.Parse() infile := flag.Arg(0) outfile := flag.Arg(1) log.Printf("%s -> %s", infile, outfile) fin, err := os.Open(infile) maybefatal(err) defer fin.Close() b := bufio.NewReader(bzip2.NewReader(fin)) fout, err := os.Create(outfile) maybefatal(err) defer fout.Close() process(b, fout) log.Printf("Wrote %s items", humanize.Comma(saw)) }