-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpar.go
115 lines (94 loc) · 2.47 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
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
"sync"
)
func main() {
template := parseCommand()
lines := make(chan string)
workers := &sync.WaitGroup{}
numWorkers := maxInt(runtime.NumCPU(), 4)
// Spin up the workers
fmt.Printf("Running with %d workers\n", numWorkers)
for i := 0; i < numWorkers; i++ {
workers.Add(1)
go work(workers, template, lines)
}
// Read standard input (while available) and queue it up on the lines channel
readFromStdin(lines)
close(lines)
// Wait for the workers to finish.
workers.Wait()
}
func parseCommand() string {
flag.Parse()
// Template of command to be run for each line of stdin
if flag.NArg() == 0 {
log.Fatal("Sorry, you must supply a command to run as the first argument.")
}
return flag.Arg(0)
}
func maxInt(a, b int) int {
if a < b {
return b
}
return a
}
func readFromStdin(lines chan<- string) {
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
for err == nil {
lines <- line
line, err = reader.ReadString('\n')
}
}
func work(wg *sync.WaitGroup, template string, lines <-chan string) {
defer wg.Done()
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
for line := range lines {
line = strings.TrimSpace(line)
command := strings.Replace(template, "%line", line, -1)
writer.WriteString(fmt.Sprintf("[%s] starting\n", command))
split_command := strings.Split(command, " ")
cmd := exec.Command(split_command[0], split_command[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil { // error getting stdout, no need to crash the whole app
log.Println(err)
}
stderr, err := cmd.StderrPipe()
if err != nil { // error getting stderr, no need to crash the whole app
log.Println(err)
}
if err := cmd.Start(); err != nil {
log.Println(err)
continue
}
// TODO: DRY this up.
reader := bufio.NewReader(stdout)
outputLine, err := reader.ReadString('\n')
for err == nil {
writer.WriteString(fmt.Sprintf("[%s] %s", command, outputLine))
outputLine, err = reader.ReadString('\n')
}
stdout.Close()
reader = bufio.NewReader(stderr)
outputLine, err = reader.ReadString('\n')
for err == nil {
writer.WriteString(fmt.Sprintf("[%s] %s", command, outputLine))
outputLine, err = reader.ReadString('\n')
}
stderr.Close()
if err := cmd.Wait(); err != nil {
writer.WriteString(fmt.Sprintf("[%s] err: %v\n", command, err))
}
writer.WriteString(fmt.Sprintf("[%s] finished\n", command))
}
}