From 3a108120368aaf539f58699c2478800ec41dfe3d Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 10 Dec 2022 09:22:25 +0000 Subject: [PATCH] added 'log' output table to lua filter --- scraping.go | 10 ++++++++++ scraping_test.go | 18 ++++++++++++++++++ todo.md | 1 - 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/scraping.go b/scraping.go index 99eb5f1..3dab370 100644 --- a/scraping.go +++ b/scraping.go @@ -811,8 +811,13 @@ func getFilterResultLua(filter *Filter) { } } L.SetGlobal("inputs", inputs) + outputs := L.CreateTable(10, 0) L.SetGlobal("outputs", outputs) + + logs := L.CreateTable(10, 0) + L.SetGlobal("logs", logs) + err := L.DoString(filter.Var1) if err != nil { filter.log(err) @@ -823,4 +828,9 @@ func getFilterResultLua(filter *Filter) { filter.Results = append(filter.Results, value.String()) }, ) + logs.ForEach( + func(key lua.LValue, value lua.LValue) { + filter.Logs = append(filter.Logs, value.String()) + }, + ) } diff --git a/scraping_test.go b/scraping_test.go index 4462f7b..8d330e7 100644 --- a/scraping_test.go +++ b/scraping_test.go @@ -1217,3 +1217,21 @@ if not(result==true) then error("regexp.match()") end` }) } } + +func TestFilterLuaLogs(t *testing.T) { + script := `table.insert(logs, "test")` + + filter := Filter{ + Var1: script, + } + + getFilterResultLua(&filter) + + if len(filter.Logs) == 0 { + t.Error("Nothing in logs, expected 'test'") + } + + if filter.Logs[0] != "test" { + t.Errorf("Unexpected log message: '%s'", filter.Logs[0]) + } +} diff --git a/todo.md b/todo.md index 41f48f9..3feba91 100644 --- a/todo.md +++ b/todo.md @@ -1,5 +1,4 @@ # Todo -- add logs table to lua filter - make generic 'notifier' interface - notify filter var2 for specific notifier or 'all' - telegram