Move computation from runtime to setup

This commit is contained in:
ppom 2023-03-24 18:06:57 +01:00
parent 39ecdca105
commit 487bcb5942
2 changed files with 8 additions and 9 deletions

View File

@ -30,7 +30,7 @@ type Filter struct {
Regex []string
compiledRegex []regexp.Regexp
patternName string
patternName, patternWithBraces string
Retry uint
RetryPeriod string `yaml:"retry-period"`
@ -80,9 +80,9 @@ func (c *Conf) setup() {
switch filter.patternName {
case "":
filter.patternName = patternName
filter.patternWithBraces = fmt.Sprintf("<%s>", patternName)
case patternName:
// no op
filter.patternName = patternName
default:
log.Fatalf(
"ERROR Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",

View File

@ -3,7 +3,6 @@ package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"os/exec"
@ -52,25 +51,25 @@ func (f *Filter) match(line string) string {
}
func (f *Filter) execActions(match string) {
pattern := fmt.Sprintf("<%s>", f.patternName)
for _, a := range f.Actions {
go a.exec(match, pattern)
go a.exec(match)
}
}
func (a *Action) exec(match, pattern string) {
func (a *Action) exec(match string) {
if a.afterDuration != 0 {
time.Sleep(a.afterDuration)
}
computedCommand := make([]string, 0, len(a.Cmd))
for _, item := range a.Cmd {
computedCommand = append(computedCommand, strings.ReplaceAll(item, pattern, match))
computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.patternWithBraces, match))
}
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:]...)
if ret := cmd.Run(); ret != nil {
log.Printf("ERR %s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
}