Skip to content

Commit

Permalink
Add --use-matchers and --use-filters flags (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
hahwul committed Oct 25, 2023
1 parent d0aa5fb commit aa44fc2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/deliver/send_elasticsearch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class SendElasticSearch < Deliver
uri.port = 9200
end

applied_endpoints = apply_all(endpoints)

body = {
"endpoints" => endpoints,
"endpoints" => applied_endpoints,
}.to_json
es_headers = @headers
es_headers["Content-Type"] = "application/json"
Expand Down
3 changes: 2 additions & 1 deletion src/deliver/send_proxy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ require "../models/deliver"
class SendWithProxy < Deliver
def run(endpoints : Array(Endpoint))
proxy_url = URI.parse(@proxy)
endpoints.each do |endpoint|
applied_endpoints = apply_all(endpoints)
applied_endpoints.each do |endpoint|
begin
if endpoint.params.size > 0
endpoint_hash = endpoint.params_to_hash
Expand Down
3 changes: 2 additions & 1 deletion src/deliver/send_req.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ require "../models/deliver"

class SendReq < Deliver
def run(endpoints : Array(Endpoint))
endpoints.each do |endpoint|
applied_endpoints = apply_all(endpoints)
applied_endpoints.each do |endpoint|
begin
if endpoint.params.size > 0
endpoint_hash = endpoint.params_to_hash
Expand Down
69 changes: 69 additions & 0 deletions src/models/deliver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Deliver
@is_log : Bool
@proxy : String
@headers : Hash(String, String) = {} of String => String
@matchers : Array(String) = [] of String
@filters : Array(String) = [] of String

def initialize(options : Hash(Symbol, String))
@options = options
Expand All @@ -29,6 +31,65 @@ class Deliver
end
@logger.info_sub "#{@headers.size} headers added."
end

@matchers = options[:use_matchers].split("::NOIR::MATCHER::SPLIT::")
@matchers.delete("")
if @matchers.size > 0
@logger.system "#{@matchers.size} matchers added."
end

@filters = options[:use_filters].split("::NOIR::FILTER::SPLIT::")
@filters.delete("")
if @filters.size > 0
@logger.system "#{@filters.size} filters added."
end
end

def apply_all(endpoints : Array(Endpoint))
result = endpoints
@logger.debug "Matchers: #{@matchers}"
@logger.debug "Filters: #{@filters}"

if @matchers.size > 0
@logger.system "Applying matchers"
result = apply_matchers(endpoints)
end

if @filters.size > 0
@logger.system "Applying filters"
result = apply_filters(endpoints)
end

result
end

def apply_matchers(endpoints : Array(Endpoint))
result = [] of Endpoint
endpoints.each do |endpoint|
@matchers.each do |matcher|
if endpoint.url.includes? matcher
@logger.debug "Endpoint '#{endpoint.url}' matched with '#{matcher}'."
result << endpoint
end
end
end

result
end

def apply_filters(endpoints : Array(Endpoint))
result = [] of Endpoint
endpoints.each do |endpoint|
@filters.each do |filter|
if endpoint.url.includes? filter
@logger.debug "Endpoint '#{endpoint.url}' filtered with '#{filter}'."
else
result << endpoint
end
end
end

result
end

def proxy
Expand All @@ -39,6 +100,14 @@ class Deliver
@headers
end

def matchers
@matchers
end

def filters
@filters
end

def run
# After inheriting the class, write an action code here.
end
Expand Down
6 changes: 6 additions & 0 deletions src/noir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ OptionParser.parse do |parser|
parser.on "--with-headers X-Header:Value", "Add Custom Headers to be Used in Deliver" do |var|
noir_options[:send_with_headers] += "#{var}::NOIR::HEADERS::SPLIT::"
end
parser.on "--use-matchers string", "Delivers URLs that match a specific condition" do |var|
noir_options[:use_matchers] += "#{var}::NOIR::MATCHER::SPLIT::"
end
parser.on "--use-filters string", "Excludes URLs that match a specific condition." do |var|
noir_options[:use_filters] += "#{var}::NOIR::FILTER::SPLIT::"
end

parser.separator "\n Technologies:".colorize(:blue)
parser.on "-t TECHS", "--techs rails,php", "Specify the technologies to use" { |var| noir_options[:techs] = var }
Expand Down
2 changes: 1 addition & 1 deletion src/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def default_options
noir_options = {
:base => "", :url => "", :format => "plain",
:output => "", :techs => "", :debug => "no", :color => "yes",
:send_proxy => "", :send_req => "no", :send_with_headers => "", :send_es => "",
:send_proxy => "", :send_req => "no", :send_with_headers => "", :send_es => "", :use_matchers => "", :use_filters => "",
:scope => "url,param", :set_pvalue => "", :nolog => "no",
:exclude_techs => "",
}
Expand Down

0 comments on commit aa44fc2

Please sign in to comment.