-
Notifications
You must be signed in to change notification settings - Fork 0
/
par.go
70 lines (54 loc) · 1.08 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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"golang.org/x/sync/errgroup"
)
const (
shell = "/bin/bash"
)
var (
dryRun = flag.Bool("n", false, "Dry run - print commands instead of executing them")
verbose = flag.Bool("v", false, "Verbose - print commands as they are executed")
)
func runCommand(command string) error {
cmd := exec.Command(shell, "-c", command)
if cmd.Err != nil {
return cmd.Err
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if *dryRun || *verbose {
fmt.Fprintf(os.Stderr, "%s\n", cmd.String())
}
if *dryRun {
return nil
}
return cmd.Run()
}
func run(ctx context.Context) error {
group, ctx := errgroup.WithContext(ctx)
group.SetLimit(runtime.NumCPU())
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
group.Go(func() error { return runCommand(line) })
}
if err := group.Wait(); err != nil {
return err
}
return scanner.Err()
}
func main() {
flag.Parse()
if err := run(context.Background()); err != nil {
log.Fatal(err)
}
}