diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..28068d0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module reaction + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..d327425 --- /dev/null +++ b/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "log" + "os/exec" +) + +type Action struct { + regex, cmd []string +} + +type Stream struct { + cmd []string + actions []Action +} + +func streamHandle(stream Stream, execQueue chan string) { + log.Printf("streamHandle{%v}: start\n", stream.cmd) + cmd := exec.Command(stream.cmd...) + stdout, err := cmd.StdoutPipe() + if err != nil { + log.Fatal("couldn't open stdout on command:", err) + } + if err := cmd.Start(); err != nil { + log.Fatal("couldn't start command:", err) + } + defer stdout.Close() + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + line := scanner.Text() + // try to match and send to execQueue if matching + } +} + +func execQueue() chan string { + queue := make(chan string) + go func() { + for { + command := <-queue + return_code := run(command) + if return_code != 0 { + log.Printf("Error launching `%v`\n", command) + } + } + }() + return queue +} + +func main() { + mockstreams := []Stream{Stream{ + []string{"tail", "-f", "/home/ao/DOWN"}, + []Action{Action{ + "prout.dev", + []string{"echo", "DAMN"}, + }}, + }} + streams := mockstreams + log.Println(streams) + queue := execQueue() + stop := make(chan bool) + for _, stream := range streams { + go streamHandle(stream, queue) + } +}