From 95ade7917c04a9b64da4e46f58adc62fcb20c576 Mon Sep 17 00:00:00 2001 From: ppom <> Date: Sun, 19 Mar 2023 23:10:18 +0100 Subject: [PATCH] first go code --- go.mod | 3 +++ main.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 go.mod create mode 100644 main.go 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) + } +}