dm/cs
1
0
forked from sm/cs

Sort output before it goes out

Fixes #1
This commit is contained in:
Stephen McQuay 2017-10-18 17:43:26 -07:00
parent 717f195cf7
commit 98d5dd7033
Signed by untrusted user: sm
GPG Key ID: 4E4B72F479BA3CE5

22
hash.go
View File

@ -9,6 +9,7 @@ import (
"hash" "hash"
"io" "io"
"os" "os"
"sort"
"sync" "sync"
) )
@ -19,6 +20,13 @@ type result struct {
err error err error
} }
// results exists to sort a slice of result
type results []result
func (r results) Len() int { return len(r) }
func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r results) Less(i, j int) bool { return r[i].f < r[j].f }
// hashr exists so that we can make a thing that can return valid hash.Hash // hashr exists so that we can make a thing that can return valid hash.Hash
// interfaces. // interfaces.
type hashr func() hash.Hash type hashr func() hash.Hash
@ -72,7 +80,19 @@ func hsh(files []string) chan result {
res = append(res, compute(h, jobs)) res = append(res, compute(h, jobs))
} }
return rmerge(res) o := make(chan result)
go func() {
rs := results{}
for r := range rmerge(res) {
rs = append(rs, r)
}
sort.Sort(rs)
for _, r := range rs {
o <- r
}
close(o)
}()
return o
} }
// compute is the checksumming workhorse // compute is the checksumming workhorse