-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
98 lines (75 loc) · 1.75 KB
/
main.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
package main
import (
"fmt"
"io"
"os"
"os/exec"
"strconv"
"sync"
flags "github.com/jessevdk/go-flags"
)
const appName = "ntimes"
var (
version = ""
gitCommit = ""
)
type options struct {
Parallels int `short:"p" long:"parallels" description:"Parallel degree of execution" default:"1"`
ShowVersion bool `short:"v" long:"version" description:"Show version"`
}
func main() {
var opts options
parser := flags.NewParser(&opts, flags.Default^flags.PrintErrors)
parser.Name = appName
parser.Usage = "N [OPTIONS] -- COMMAND"
args, err := parser.Parse()
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok {
if flagsErr.Type == flags.ErrHelp {
parser.WriteHelp(os.Stderr)
return
}
}
errorf("flag parse error: %s", err)
os.Exit(1)
}
if opts.ShowVersion {
_, _ = io.WriteString(os.Stdout, fmt.Sprintf("%s v%s, build %s\n", appName, version, gitCommit))
return
}
cnt, err := strconv.Atoi(args[0])
cmdName := args[1]
cmdArgs := args[2:]
if err != nil {
panic(err)
}
ntimes(cnt, cmdName, cmdArgs, os.Stdin, os.Stdout, os.Stderr, opts.Parallels)
}
func ntimes(cnt int, cmdName string, cmdArgs []string, stdin io.Reader, stdout io.Writer, stderr io.Writer, parallels int) {
var wg sync.WaitGroup
sema := make(chan bool, parallels)
for i := 0; i < cnt; i++ {
wg.Add(1)
go func() {
sema <- true
defer func() {
wg.Done()
<-sema
}()
cmd := exec.Command(cmdName, cmdArgs...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
if err != nil {
panic(err)
}
}()
}
wg.Wait()
close(sema)
}
func errorf(message string, args ...interface{}) {
subMessage := fmt.Sprintf(message, args...)
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", appName, subMessage)
}