From 487bcb5942211b9fbc1e2e34084e6ced57ff17d3 Mon Sep 17 00:00:00 2001 From: ppom <> Date: Fri, 24 Mar 2023 18:06:57 +0100 Subject: [PATCH] Move computation from runtime to setup --- conf.go | 8 ++++---- main.go | 9 ++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/conf.go b/conf.go index 791e101..d9224d1 100644 --- a/conf.go +++ b/conf.go @@ -28,9 +28,9 @@ type Filter struct { stream *Stream name string - Regex []string - compiledRegex []regexp.Regexp - patternName string + Regex []string + compiledRegex []regexp.Regexp + 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", diff --git a/main.go b/main.go index 7314dd0..5190a30 100644 --- a/main.go +++ b/main.go @@ -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) }