-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmd_regexp.go
66 lines (60 loc) · 1.46 KB
/
cmd_regexp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gotesplit
import (
"context"
"fmt"
"io"
"math"
"strconv"
"strings"
)
type cmdRegexp struct{}
func (c *cmdRegexp) run(ctx context.Context, argv []string, outStream io.Writer, errStream io.Writer) error {
// FIXME:
if len(argv) < 3 {
return fmt.Errorf("not enough arguments")
}
pkgs := argv[2:]
total, err := strconv.Atoi(argv[0])
if err != nil {
return fmt.Errorf("invalid total: %s", err)
}
idx, err := strconv.Atoi(argv[1])
if err != nil {
return fmt.Errorf("invalid index: %s", err)
}
str, err := getOut(pkgs, detectTags(argv), detectRace(argv), total, idx)
if err != nil {
return err
}
_, err = fmt.Fprintln(outStream, str)
return err
}
func getOut(pkgs []string, tags string, withRace bool, total, idx int) (string, error) {
if total < 1 {
return "", fmt.Errorf("invalid total: %d", total)
}
if idx >= total {
return "", fmt.Errorf("index shoud be between 0 to total-1, but: %d (total:%d)", idx, total)
}
testLists, err := getTestListsFromPkgs(pkgs, tags, withRace)
if err != nil {
return "", err
}
var list []string
if len(testLists) > 0 {
list = testLists[0].list
}
testNum := len(list)
minMemberPerGroup := testNum / total
mod := testNum % total
getOffset := func(i int) int {
return minMemberPerGroup*i + int(math.Min(float64(i), float64(mod)))
}
from := getOffset(idx)
to := getOffset(idx + 1)
s := list[from:to]
if len(s) == 0 {
return "0^", nil
}
return "^(?:" + strings.Join(list[from:to], "|") + ")$", nil
}