-
Notifications
You must be signed in to change notification settings - Fork 0
/
par.go
106 lines (97 loc) · 1.99 KB
/
par.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"sync"
)
type job struct {
name string
cmd []string
out string
err error
}
var runners = flag.Int("j", 20, "number of concurrent jobs")
var retries = flag.Int("r", 1, "try failing jobs this many times")
var replace = flag.String("i", "", "arg pattern to be replaced with inputs")
var bare = flag.Bool("b", false, "show bare output without prefix")
var verbose = flag.Bool("v", false, "verbose output, show task start/finish and exit status")
func runner(in chan *job, out chan *job) {
for j := range in {
for try := 0; try < *retries; try++ {
if *verbose {
fmt.Printf("%s: STARTING\n", j.name)
}
c := exec.Command(j.cmd[0], j.cmd[1:]...)
o, err := c.CombinedOutput()
j.err = err
j.out = string(o)
if *verbose {
if err == nil {
fmt.Printf("%s: SUCCEES\n", j.name)
} else {
fmt.Printf("%s: FAILED: %s\n", j.name, err)
}
}
if err == nil {
break
}
}
out <- j
}
}
func printer(in <-chan *job, wg *sync.WaitGroup) {
for j := range in {
if j.err != nil {
fmt.Printf("%s: %s\n", j.name, j.err.Error())
}
lines := strings.Split(j.out, "\n")
for _, l := range lines {
if l != "" {
if *bare {
fmt.Println(l)
} else {
fmt.Printf("%s: %s\n", j.name, l)
}
}
}
wg.Done()
}
}
func main() {
flag.Parse()
preargs := []string{"/bin/echo"}
if flag.NArg() > 0 {
preargs = flag.Args()
}
submitter := make(chan *job)
results := make(chan *job, 1000)
wg := &sync.WaitGroup{}
go printer(results, wg)
for i := 0; i < *runners; i++ {
go runner(submitter, results)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
l := scanner.Text()
cmd := make([]string, 0)
if *replace == "" {
cmd = append(preargs, l)
} else {
for _, a := range preargs {
cmd = append(cmd, strings.Replace(a, *replace, l, -1))
}
}
j := &job{
name: l,
cmd: cmd,
}
wg.Add(1)
submitter <- j
}
close(submitter)
wg.Wait()
}