-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrip.go
70 lines (56 loc) · 1.31 KB
/
rip.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 (
"flag"
"io"
"log"
"os"
"os/exec"
"strconv"
)
func checkError(err error) {
if err != nil {
log.Fatalf("Error: %s", err)
}
}
func main() {
// initialize commandline flags
numProc := flag.Int("n", 4, "number of processes")
wordCmd := flag.String("c", "", "command")
// parse flags
flag.Parse()
// check for empty command
if (*wordCmd == "") {
log.Fatalf("Error: Command not given")
os.Exit(2)
}
args := flag.Args()
// check for _seed_ in tail save index position of seed
seedindex := -1
for index, element := range args {
if element == "_seed_" {
seedindex = index
}
}
// launch as many processes as wanted
for i := 0; i < *numProc; i++ {
// replace _seed_ by i
if seedindex != -1 {
args[seedindex] = strconv.Itoa(i)
}
// the tail is given as arguments to the command which is run
cmd := exec.Command(*wordCmd, args...)
// create stdout, stderr streams of type io.Reader
stdout, err := cmd.StdoutPipe()
checkError(err)
stderr, err := cmd.StderrPipe()
checkError(err)
// non-blockingly echo command output to terminal
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
// start command
err = cmd.Start()
checkError(err)
// don't let main() exit before our command has finished running
defer cmd.Wait() // doesn't block
}
}