From af2f092b718dde4a847347f1be9e9126aaaf09b2 Mon Sep 17 00:00:00 2001 From: ppom Date: Tue, 9 Jan 2024 12:00:00 +0100 Subject: [PATCH] Fix crash when all the regexes of a filter have no pattern in them Fix #66 Example configuration causing crash: ```yaml streams: mongod_restart: cmd: [ 'sh', '-c', 'while true; do echo "Started MongoDB Database Server."; sleep 5; done' ] filters: mongo_restart: regex: - 'Started MongoDB Database Server.' actions: restart_chat_tel: cmd: [ 'systemctl', 'restart', 'nginx' ] ``` --- app/daemon.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/daemon.go b/app/daemon.go index 1bb1ab5..f249c83 100644 --- a/app/daemon.go +++ b/app/daemon.go @@ -76,11 +76,17 @@ func (f *Filter) match(line *string) string { if matches := regex.FindStringSubmatch(*line); matches != nil { - match := matches[regex.SubexpIndex(f.pattern.name)] + if f.pattern != nil { + match := matches[regex.SubexpIndex(f.pattern.name)] - if f.pattern.notAnIgnore(&match) { - logger.Printf(logger.INFO, "%s.%s: match [%v]\n", f.stream.name, f.name, match) - return match + if f.pattern.notAnIgnore(&match) { + logger.Printf(logger.INFO, "%s.%s: match [%v]\n", f.stream.name, f.name, match) + return match + } + } else { + logger.Printf(logger.INFO, "%s.%s: match [.]\n", f.stream.name, f.name) + // No pattern, so this match will never actually be used + return "." } } } @@ -96,9 +102,16 @@ func (f *Filter) sendActions(match string, at time.Time) { func (a *Action) exec(match string) { defer wgActions.Done() - computedCommand := make([]string, 0, len(a.Cmd)) - for _, item := range a.Cmd { - computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.pattern.nameWithBraces, match)) + var computedCommand []string + + if a.filter.pattern != nil { + computedCommand := make([]string, 0, len(a.Cmd)) + + for _, item := range a.Cmd { + computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.pattern.nameWithBraces, match)) + } + } else { + computedCommand = a.Cmd } logger.Printf(logger.INFO, "%s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)