Skip to content

Commit

Permalink
Added: Parse regex captures for Discord embed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
danielunderwood committed Mar 2, 2022
1 parent 141e038 commit a7df7f8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<host>\w+) sshd\[\d+\]: Accepted publickey for (?P<user>\w+) from (?P<source>[\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{
Expand Down

0 comments on commit a7df7f8

Please sign in to comment.