first go code

This commit is contained in:
ppom 2023-03-19 23:10:18 +01:00
parent e134e2c9ce
commit 95ade7917c
2 changed files with 67 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module reaction
go 1.19

64
main.go Normal file
View File

@ -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)
}
}