From a7df7f833ac02f78e2b2879b2ac05040d9e788c6 Mon Sep 17 00:00:00 2001 From: Daniel Underwood Date: Tue, 1 Mar 2022 20:35:18 -0500 Subject: [PATCH] Added: Parse regex captures for Discord embed fields --- main.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 8312775..04266f4 100644 --- a/main.go +++ b/main.go @@ -72,16 +72,28 @@ func main() { } for line := range t.Lines { - fields := make([]Field, 2) + fields := make([]Field, 0, len(re.SubexpNames())+2) fields = append(fields, Field{Name: "source", Value: sourceName}) fields = append(fields, Field{Name: "file", Value: file}) - matched, err := regexp.MatchString(expression, line.Text) + // This allows to use groups within the regex to find things like + // nix run .#log2http -- -file fakefile -regexp '(?P\w+) sshd\[\d+\]: Accepted publickey for (?P\w+) from (?P[\d\.]+)' + // and use them as fields + match := re.FindStringSubmatch(line.Text) + if len(match) == 0 { + continue + } + for i, name := range re.SubexpNames() { + if i != 0 && name != "" { + fields = append(fields, Field{Name: name, Value: match[i]}) + } + } + if err != nil { fmt.Println("ERROR", err) continue } - if matched { + if len(match) > 0 { fmt.Println("Matched", line.Text) message := DiscordMessage{ Embeds: []Embed{