first working prototype

This commit is contained in:
ppom 2023-03-20 23:25:57 +01:00
parent 95ade7917c
commit e87c09283d

54
main.go
View File

@ -1,22 +1,40 @@
package main package main
import ( import (
"bufio"
"log" "log"
"os/exec" "os/exec"
"regexp"
) )
type Action struct { type Action struct {
regex, cmd []string regex, cmd []string
} }
type compiledAction struct {
regex []regexp.Regexp
cmd []string
}
type Stream struct { type Stream struct {
cmd []string cmd []string
actions []Action actions []Action
} }
func streamHandle(stream Stream, execQueue chan string) { func compileAction(action Action) compiledAction {
var ca compiledAction
ca.cmd = action.cmd
for _, regex := range action.regex {
ca.regex = append(ca.regex, *regexp.MustCompile(regex))
}
return ca
}
/// Handle a log command
/// Must be started in a goroutine
func streamHandle(stream Stream, execQueue chan []string) {
log.Printf("streamHandle{%v}: start\n", stream.cmd) log.Printf("streamHandle{%v}: start\n", stream.cmd)
cmd := exec.Command(stream.cmd...) cmd := exec.Command(stream.cmd[0], stream.cmd[1:]...)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
log.Fatal("couldn't open stdout on command:", err) log.Fatal("couldn't open stdout on command:", err)
@ -25,21 +43,34 @@ func streamHandle(stream Stream, execQueue chan string) {
log.Fatal("couldn't start command:", err) log.Fatal("couldn't start command:", err)
} }
defer stdout.Close() defer stdout.Close()
compiledActions := make([]compiledAction, 0, len(stream.actions))
for _, action := range stream.actions {
compiledActions = append(compiledActions, compileAction(action))
}
scanner := bufio.NewScanner(stdout) scanner := bufio.NewScanner(stdout)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
// try to match and send to execQueue if matching for _, action := range compiledActions {
for _, regex := range action.regex {
if match := regex.FindString(line); match != "" {
log.Printf("match `%v` in line: `%v`\n", regex.String(), line)
execQueue <- action.cmd
}
}
}
} }
} }
func execQueue() chan string { func execQueue() chan []string {
queue := make(chan string) queue := make(chan []string)
go func() { go func() {
for { for {
command := <-queue command := <-queue
return_code := run(command) cmd := exec.Command(command[0], command[1:]...)
if return_code != 0 { if ret := cmd.Run(); ret != nil {
log.Printf("Error launching `%v`\n", command) log.Printf("Error launching `%v`: code %v\n", cmd, ret)
} }
} }
}() }()
@ -50,15 +81,16 @@ func main() {
mockstreams := []Stream{Stream{ mockstreams := []Stream{Stream{
[]string{"tail", "-f", "/home/ao/DOWN"}, []string{"tail", "-f", "/home/ao/DOWN"},
[]Action{Action{ []Action{Action{
"prout.dev", []string{"prout.dev"},
[]string{"echo", "DAMN"}, []string{"touch", "/home/ao/DAMN"},
}}, }},
}} }}
streams := mockstreams streams := mockstreams
log.Println(streams) log.Println(streams)
queue := execQueue() queue := execQueue()
stop := make(chan bool)
for _, stream := range streams { for _, stream := range streams {
go streamHandle(stream, queue) go streamHandle(stream, queue)
} }
// Infinite wait
<-make(chan bool)
} }