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

View File

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