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-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
|
|
|
|
compiledRegex []regexp.Regexp
|
|
|
|
Retry uint
|
|
|
|
RetryPeriod string `yaml:"retry-period"`
|
|
|
|
Actions map[string]*Action
|
|
|
|
}
|
|
|
|
|
|
|
|
type Action struct {
|
|
|
|
name, filterName, streamName string
|
|
|
|
Cmd []string
|
|
|
|
After string `yaml:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conf) setup() {
|
|
|
|
for streamName, stream := range c.Streams {
|
|
|
|
for filterName, filter := range stream.Filters {
|
|
|
|
// Compute Regexes
|
|
|
|
for _, regex := range filter.Regex {
|
|
|
|
filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
|
|
|
|
}
|
|
|
|
// Give all relevant infos to Actions
|
|
|
|
for actionName, action := range filter.Actions {
|
|
|
|
action.name = actionName
|
|
|
|
action.filterName = filterName
|
|
|
|
action.streamName = streamName
|
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
|
|
|
|
}
|