reaction/conf.go

136 lines
2.8 KiB
Go
Raw Normal View History

2023-03-23 21:14:53 +01:00
package main
import (
// "flag"
2023-03-24 00:27:51 +01:00
"fmt"
2023-03-23 21:14:53 +01:00
"log"
"os"
2023-03-24 00:27:51 +01:00
"regexp"
2023-03-24 17:36:41 +01:00
"strings"
2023-03-24 00:49:59 +01:00
"time"
2023-03-23 21:14:53 +01:00
"gopkg.in/yaml.v3"
)
type Conf struct {
2023-03-24 17:36:41 +01:00
Patterns map[string]string
Streams map[string]*Stream
2023-03-24 00:27:51 +01:00
}
type Stream struct {
2023-03-24 17:36:41 +01:00
name string
2023-03-24 00:27:51 +01:00
Cmd []string
Filters map[string]*Filter
}
type Filter struct {
2023-03-24 17:36:41 +01:00
stream *Stream
name string
2023-03-24 18:06:57 +01:00
Regex []string
compiledRegex []regexp.Regexp
patternName, patternWithBraces string
2023-03-24 17:36:41 +01:00
2023-03-24 00:27:51 +01:00
Retry uint
RetryPeriod string `yaml:"retry-period"`
2023-03-24 17:36:41 +01:00
retryDuration time.Duration
Actions map[string]*Action
2023-03-24 00:27:51 +01:00
}
type Action struct {
2023-03-24 17:36:41 +01:00
filter *Filter
name string
Cmd []string
After string `yaml:",omitempty"`
afterDuration time.Duration
2023-03-24 00:27:51 +01:00
}
func (c *Conf) setup() {
2023-03-24 17:36:41 +01:00
for patternName, pattern := range c.Patterns {
c.Patterns[patternName] = fmt.Sprintf("(?P<%s>%s)", patternName, pattern)
}
for streamName := range c.Streams {
stream := c.Streams[streamName]
stream.name = streamName
for filterName := range stream.Filters {
filter := stream.Filters[filterName]
filter.stream = stream
filter.name = filterName
2023-03-24 00:49:59 +01:00
// Parse Duration
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
if err != nil {
log.Fatalln("Failed to parse time in configuration file:", err)
}
filter.retryDuration = retryDuration
2023-03-24 00:27:51 +01:00
// Compute Regexes
2023-03-24 17:36:41 +01:00
// Look for Patterns inside Regexes
2023-03-24 00:27:51 +01:00
for _, regex := range filter.Regex {
2023-03-24 17:36:41 +01:00
for patternName, pattern := range c.Patterns {
if strings.Contains(regex, patternName) {
switch filter.patternName {
case "":
filter.patternName = patternName
2023-03-24 18:06:57 +01:00
filter.patternWithBraces = fmt.Sprintf("<%s>", patternName)
2023-03-24 17:36:41 +01:00
case patternName:
// no op
default:
log.Fatalf(
"ERROR Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",
filter.patternName, patternName, streamName, filterName,
)
}
regex = strings.Replace(regex, fmt.Sprintf("<%s>", patternName), pattern, 1)
}
}
2023-03-24 00:27:51 +01:00
filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
}
2023-03-24 00:49:59 +01:00
2023-03-24 17:36:41 +01:00
for actionName := range filter.Actions {
2023-03-24 00:49:59 +01:00
2023-03-24 17:36:41 +01:00
action := filter.Actions[actionName]
action.filter = filter
2023-03-24 00:27:51 +01:00
action.name = actionName
2023-03-24 00:49:59 +01:00
// Parse Duration
if action.After != "" {
afterDuration, err := time.ParseDuration(action.After)
if err != nil {
log.Fatalln("Failed to parse time in configuration file:", err)
}
action.afterDuration = afterDuration
}
2023-03-23 21:14:53 +01:00
}
}
}
}
func parseConf(filename string) *Conf {
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalln("Failed to read configuration file:", err)
}
var conf Conf
err = yaml.Unmarshal(data, &conf)
if err != nil {
log.Fatalln("Failed to parse configuration file:", err)
}
2023-03-24 00:27:51 +01:00
conf.setup()
2023-03-23 21:14:53 +01:00
return &conf
}