11 type batchArgs struct {
16 func (b *batchArgs) Flags(flags *flag.FlagSet) {
17 flags.IntVar(&b.batches, "batches", 1, "number of batches")
18 flags.IntVar(&b.batch, "batch", -1, "only do `N`th batch (-1 = all)")
21 func (b *batchArgs) Args(batch int) []string {
23 fmt.Sprintf("-batches=%d", b.batches),
24 fmt.Sprintf("-batch=%d", batch),
28 // RunBatches calls runFunc once per batch, and returns a slice of
29 // return values and the first returned error, if any.
30 func (b *batchArgs) RunBatches(ctx context.Context, runFunc func(context.Context, int) (string, error)) ([]string, error) {
31 ctx, cancel := context.WithCancel(ctx)
33 outputs := make([]string, b.batches)
35 for batch := 0; batch < b.batches; batch++ {
36 if b.batch >= 0 && b.batch != batch {
43 out, err := runFunc(ctx, batch)
53 outputs = outputs[b.batch : b.batch+1]
58 func (b *batchArgs) Slice(in []string) []string {
59 if b.batches == 0 || b.batch < 0 {
62 batchsize := (len(in) + b.batches - 1) / b.batches
63 out := in[batchsize*b.batch:]
64 if len(out) > batchsize {
70 type WaitGroup struct {
76 func (wg *WaitGroup) Error(err error) {
78 wg.errOnce.Do(func() { wg.err = err })
82 func (wg *WaitGroup) Wait() error {