-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
51 lines (43 loc) · 912 Bytes
/
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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"time"
)
func main() {
log.SetFlags(log.Lshortfile)
log.SetPrefix("again: ")
fail := flag.Bool("fail", false, "run until command fails")
sleep := flag.Duration("sleep", 0, "how long to sleep before running again?")
silent := flag.Bool("silent", false, "don't print failed attempts error message")
flag.Parse()
cmdname := flag.Arg(0)
if cmdname == "" {
fmt.Println("usage:", "again [options] cmdname [cmdargs...]")
os.Exit(1)
}
errbuf := new(bytes.Buffer)
for {
cmd := exec.Command(cmdname, flag.Args()[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = errbuf
err := cmd.Run()
if err != nil && !*fail {
if !*silent {
log.Print(err)
fmt.Print(errbuf.String())
errbuf.Reset()
}
} else if err != nil || !*fail {
break
}
if *sleep != 0 {
time.Sleep(*sleep)
}
}
}