-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (129 loc) · 3.33 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
"time"
)
func main() {
pattern_flag := flag.String("p", "", "Pattern to match (required)")
timeout_flag := flag.Duration("t", 3*time.Second, "Timeout duration. Set to 0 for no timeout.")
kill_flag := flag.Bool("k", true, "Whether the process should be terminated on pattern matched.")
flag.Usage = func() {
fmt.Print("usage: ", os.Args[0], " [flags] <command>\n\n")
fmt.Println("flags:")
flag.PrintDefaults()
}
flag.Parse()
if *pattern_flag == "" {
warn("Error: Pattern flag is required.")
flag.PrintDefaults()
os.Exit(1)
}
if flag.NArg() < 1 {
warn("Error: Command argument is required.")
flag.PrintDefaults()
os.Exit(1)
}
command := strings.Join(flag.Args(), " ")
err := executeCommandUntilMatch(command, *pattern_flag, *timeout_flag, *kill_flag)
if err != nil {
warn("Error:", err)
os.Exit(1)
}
}
func executeCommandUntilMatch(command string, pattern string, timeout time.Duration, do_kill bool) error {
command_split := strings.Split(command, " ")
cmd := exec.Command(command_split[0], command_split[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
done := make(chan bool)
pattern_match := make(chan string)
go listenForPatternMatches(stdout, "stdout", pattern, pattern_match, done)
go listenForPatternMatches(stderr, "stderr", pattern, pattern_match, nil)
if err := cmd.Start(); err != nil {
return err
}
if timeout == 0 {
select {
case line := <-pattern_match:
return returnMatch(line, cmd, do_kill)
case <-done:
return returnNoMatch()
}
} else {
select {
case line := <-pattern_match:
return returnMatch(line, cmd, do_kill)
case <-done:
return returnNoMatch()
case <-time.After(timeout):
return returnTimeout(cmd, do_kill)
}
}
}
func listenForPatternMatches(reader io.ReadCloser, scanner_type string, pattern string, pattern_match chan string, done chan bool) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
log(scanner_type, ":", line)
matched, err := regexp.MatchString(pattern, line)
if err != nil {
warn("Regex matching error", scanner_type, ":", err)
}
if matched {
pattern_match <- line
return
}
}
if err := scanner.Err(); err != nil {
warn("Scanner error", scanner_type, ":", err)
}
if done != nil {
done <- true
}
}
const (
ERROR_PATTERN_NOT_FOUND = "command completed without a match"
ERROR_TIMEOUT = "timeout reached, pattern not matched"
)
func maybeKillProcess(do_kill bool, cmd *exec.Cmd) {
if do_kill {
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGINT); err != nil {
warn("failed attempting to kill running command", err)
}
log("killed the running command")
}
}
func returnTimeout(cmd *exec.Cmd, do_kill bool) error {
maybeKillProcess(do_kill, cmd)
return errors.New(ERROR_TIMEOUT)
}
func returnMatch(line string, cmd *exec.Cmd, do_kill bool) error {
maybeKillProcess(do_kill, cmd)
log("pattern matched", line)
return nil
}
func returnNoMatch() error {
return errors.New(ERROR_PATTERN_NOT_FOUND)
}
func warn(things ...any) {
fmt.Fprintln(os.Stderr, things...)
}
func log(things ...any) {
fmt.Println(things...)
}