diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-05-29 13:53:53 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-05-29 13:53:53 +0100 |
commit | 680fd4843c6e0d2b7c33c56828573d09388be420 (patch) | |
tree | 4029f468b4c5b96254cdbde233a9644a58ab0af4 /lualib | |
parent | 6f1ac61c711a6bfcadc3f9072a9b172fb0c244a3 (diff) | |
download | rspamd-680fd4843c6e0d2b7c33c56828573d09388be420.tar.gz rspamd-680fd4843c6e0d2b7c33c56828573d09388be420.zip |
[Project] Rework grep to be plain Lua command
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/rspamadm/grep.lua | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/lualib/rspamadm/grep.lua b/lualib/rspamadm/grep.lua index a9d4b084a..7d0341e5b 100644 --- a/lualib/rspamadm/grep.lua +++ b/lualib/rspamadm/grep.lua @@ -1,6 +1,59 @@ -return function(_, res) +--[[ +Copyright (c) 2018, Vsevolod Stakhov <vsevolod@highsecure.ru> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +]]-- + +local argparse = require "argparse" + + +-- Define command line options +local parser = argparse() + :name "rspamadm grep" + :description "rspamadm grep - search for patterns in rspamd logs" + :help_description_margin(30) +parser:option "-s --string" + :description('Plain string to search (case-insensitive)') + :argname "<str>" +parser:flag "-l --lua" + :description('Use Lua patterns in string search') +parser:option "-p --pattern" + :description('Pattern to search for (regex)') + :args(1) + :argname "<re>" +parser:argument "input":args "*" + :description('Process specified inputs') + :default("stdin") +parser:flag "-S --sensitive" + :description('Enable case-sensitivity in string search') + :default("false") +parser:flag "-o --orphans" + :description('Print orphaned logs') +parser:flag "-P --partial" + :description('Print partial logs') + +local function handler(args) local rspamd_regexp = require 'rspamd_regexp' + local res = parser:parse(args) + + if not res['string'] and not res['pattern'] then + parser:error('string or pattern options must be specified') + end + + if res['string'] and res['pattern'] then + parser:error('string and pattern options are mutually exclusive') + end local buffer = {} local matches = {} @@ -16,7 +69,7 @@ return function(_, res) end local plainm = true - if res['luapat'] then + if res['lua'] then plainm = false end local orphans = res['orphans'] @@ -26,7 +79,7 @@ return function(_, res) if search_str and not sensitive then search_str = string.lower(search_str) end - local inputs = res['inputs'] + local inputs = res['input'] or {'stdin'} for _, n in ipairs(inputs) do local h, err @@ -110,3 +163,9 @@ return function(_, res) end end end + +return { + handler = handler, + description = parser.description, + name = 'grep' +}
\ No newline at end of file |