reaction/conf.go

99 lines
2.2 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 00:49:59 +01:00
"time"
2023-03-23 21:14:53 +01:00
"gopkg.in/yaml.v3"
)
type Conf struct {
2023-03-24 00:27:51 +01:00
Streams map[string]Stream
}
type Stream struct {
Cmd []string
Filters map[string]*Filter
}
type Filter struct {
Regex []string
2023-03-24 00:49:59 +01:00
compiledRegex []regexp.Regexp // Computed after YAML parsing
2023-03-24 00:27:51 +01:00
Retry uint
RetryPeriod string `yaml:"retry-period"`
2023-03-24 00:49:59 +01:00
retryDuration time.Duration // Computed after YAML parsing
2023-03-24 00:27:51 +01:00
Actions map[string]*Action
}
type Action struct {
2023-03-24 00:49:59 +01:00
name, filterName, streamName string // Computed after YAML parsing
2023-03-24 00:27:51 +01:00
Cmd []string
After string `yaml:",omitempty"`
2023-03-24 00:49:59 +01:00
afterDuration time.Duration // Computed after YAML parsing
2023-03-24 00:27:51 +01:00
}
func (c *Conf) setup() {
for streamName, stream := range c.Streams {
for filterName, filter := range stream.Filters {
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
for _, regex := range filter.Regex {
filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
}
2023-03-24 00:49:59 +01:00
2023-03-24 00:27:51 +01:00
for actionName, action := range filter.Actions {
2023-03-24 00:49:59 +01:00
// Give all relevant infos to Actions
2023-03-24 00:27:51 +01:00
action.name = actionName
action.filterName = filterName
action.streamName = streamName
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()
fmt.Printf("conf.Streams[0].Filters[0].Actions: %s\n", conf.Streams["tailDown"].Filters["lookForProuts"].Actions)
2023-03-23 21:14:53 +01:00
return &conf
}
func parseArgs() map[string]string {
var args map[string]string
return args
}