reaction/app/reaction.go

157 lines
3.2 KiB
Go
Raw Normal View History

2023-03-25 18:27:01 +01:00
package app
2023-03-19 23:10:18 +01:00
import (
2023-03-20 23:25:57 +01:00
"bufio"
2023-03-24 17:52:00 +01:00
"flag"
// "fmt"
2023-03-19 23:10:18 +01:00
"log"
2023-03-24 17:52:00 +01:00
"os"
2023-03-19 23:10:18 +01:00
"os/exec"
2023-03-24 17:36:41 +01:00
"strings"
"sync"
2023-03-24 17:36:41 +01:00
"time"
2023-03-19 23:10:18 +01:00
)
2023-03-24 17:36:41 +01:00
// Executes a command and channel-send its stdout
func cmdStdout(commandline []string) chan *string {
lines := make(chan *string)
2023-03-19 23:10:18 +01:00
2023-03-24 00:27:51 +01:00
go func() {
cmd := exec.Command(commandline[0], commandline[1:]...)
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()
2023-03-20 23:25:57 +01:00
2023-03-24 00:27:51 +01:00
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
lines <- &line
2023-03-24 00:27:51 +01:00
}
close(lines)
}()
return lines
2023-03-19 23:10:18 +01:00
}
2023-03-24 17:36:41 +01:00
// Whether one of the filter's regexes is matched on a line
func (f *Filter) match(line *string) string {
2023-03-24 00:27:51 +01:00
for _, regex := range f.compiledRegex {
2023-03-24 17:36:41 +01:00
if matches := regex.FindStringSubmatch(*line); matches != nil {
2023-03-24 17:36:41 +01:00
match := matches[regex.SubexpIndex(f.patternName)]
log.Printf("INFO %s.%s: match [%v]\n", f.stream.name, f.name, match)
2023-03-24 17:36:41 +01:00
return match
2023-03-24 00:27:51 +01:00
}
2023-03-20 23:25:57 +01:00
}
2023-03-24 17:36:41 +01:00
return ""
2023-03-20 23:25:57 +01:00
}
2023-03-24 17:36:41 +01:00
func (f *Filter) execActions(match string) {
2023-03-24 00:27:51 +01:00
for _, a := range f.Actions {
wgActions.Add(1)
2023-03-24 18:06:57 +01:00
go a.exec(match)
2023-03-19 23:10:18 +01:00
}
2023-03-24 00:27:51 +01:00
}
2023-03-20 23:25:57 +01:00
2023-03-24 18:06:57 +01:00
func (a *Action) exec(match string) {
defer wgActions.Done()
2023-03-24 00:49:59 +01:00
if a.afterDuration != 0 {
time.Sleep(a.afterDuration)
}
2023-03-20 23:25:57 +01:00
2023-03-24 17:36:41 +01:00
computedCommand := make([]string, 0, len(a.Cmd))
for _, item := range a.Cmd {
2023-03-24 18:06:57 +01:00
computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.patternWithBraces, match))
2023-03-24 17:36:41 +01:00
}
log.Printf("INFO %s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)
cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
2023-03-24 18:06:57 +01:00
2023-03-24 00:27:51 +01:00
if ret := cmd.Run(); ret != nil {
2023-03-24 17:36:41 +01:00
log.Printf("ERR %s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
2023-03-19 23:10:18 +01:00
}
}
func (f *Filter) handle() chan *string {
lines := make(chan *string)
go func() {
for line := range lines {
if match := f.match(line); match != "" {
f.execActions(match)
}
}
}()
return lines
}
func multiplex(input chan *string, outputs []chan *string) {
var wg sync.WaitGroup
for item := range input {
for _, output := range outputs {
wg.Add(1)
go func(s *string) {
output <- s
wg.Done()
}(item)
}
}
for _, output := range outputs {
wg.Wait()
close(output)
}
}
func (s *Stream) handle(signal chan *Stream) {
2023-03-24 17:36:41 +01:00
log.Printf("INFO %s: start %s\n", s.name, s.Cmd)
2023-03-24 00:27:51 +01:00
lines := cmdStdout(s.Cmd)
var filterInputs = make([]chan *string, 0, len(s.Filters))
for _, filter := range s.Filters {
filterInputs = append(filterInputs, filter.handle())
2023-03-24 00:27:51 +01:00
}
multiplex(lines, filterInputs)
signal <- s
2023-03-19 23:10:18 +01:00
}
var wgActions sync.WaitGroup
func Main() {
2023-03-24 17:52:00 +01:00
confFilename := flag.String("c", "", "configuration file. see an example at https://framagit.org/ppom/reaction/-/blob/main/reaction.yml")
flag.Parse()
if *confFilename == "" {
flag.PrintDefaults()
os.Exit(2)
}
conf := parseConf(*confFilename)
2023-03-24 00:27:51 +01:00
endSignals := make(chan *Stream)
2023-03-24 00:27:51 +01:00
for _, stream := range conf.Streams {
go stream.handle(endSignals)
}
for i := 0; i < len(conf.Streams); i++ {
finishedStream := <-endSignals
log.Printf("ERR %s stream finished", finishedStream.name)
2023-03-24 00:27:51 +01:00
}
wgActions.Wait()
os.Exit(3)
2023-03-19 23:10:18 +01:00
}