Skip to content

Commit

Permalink
Rework CLI options parsing
Browse files Browse the repository at this point in the history
Add support for missing options, simplify interface and add short
options for frequently used options.
  • Loading branch information
smortex committed Jan 30, 2024
1 parent 16526ee commit 41a8fbf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
29 changes: 16 additions & 13 deletions exe/email-report-processor
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ options = {
class: nil,
mbox: false,
os: {
hostname: 'localhost',
port: 9200,
host: 'https://localhost:9200',
username: 'admin',
password: 'admin',
},
Expand All @@ -21,28 +20,32 @@ options = {
OptionParser.new do |opts|
opts.banner = "usage: #{$PROGRAM_NAME} [options] [file...]"
opts.separator("\nReport type selection:")
opts.on('--dmarc', 'Process DMARC Reports') do
opts.on('-d', '--dmarc', 'Process DMARC Reports') do
options[:class] = EmailReportProcessor::DmarcRua
end
opts.on('--tlsrpt', 'Process SMTP TLS Reports') do
opts.on('-t', '--tlsrpt', 'Process SMTP TLS Reports') do
options[:class] = EmailReportProcessor::TlsrptRua
end

opts.separator("\nOpenSearch options:")
opts.on('-h', '--os-hostname=HOSTNAME', 'Hostname of the OpenSearch instance') do |hostname|
options[:os][:hostname] = hostname
end
opts.on('-p', '--os-port=PORT', 'Port of the OpenSearch instance') do |port|
options[:os][:port] = port.to_i
opts.on('-h', '--os-host=HOSTNAME', 'Hostname of the OpenSearch instance') do |host|
options[:os][:host] = host
end
opts.on('-u', '--os-username=USERNAME', 'Username of the OpenSearch instance') do |username|
options[:os][:username] = username
end
opts.on('--os-password=PASSWORD', 'Password of the OpenSearch instance') do |password|
opts.on('-p', '--os-password=PASSWORD', 'Password of the OpenSearch instance') do |password|
options[:os][:password] = password
end
opts.on('--dmarc-index=INDEX', 'OpenSearch index of DMARC reports') do |index|
options[:dmarc_index] = index
end
opts.on('--tlsrpt-index=INDEX', 'OpenSearch index of SMTP TLS reports') do |index|
options[:tlsrpt_index] = index
end

opts.separator("\nMiscellaneous options:")
opts.on('--mbox', 'Treat the provided file as a mailbox') do
opts.on('-m', '--mbox', 'Treat the provided file as a mailbox') do
options[:mbox] = true
end
end.parse!
Expand All @@ -53,13 +56,13 @@ if options[:class].nil?
end

client = OpenSearch::Client.new(
host: "https://#{options[:os][:hostname]}:#{options[:os][:port]}",
host: options[:os][:host],
user: options[:os][:username],
password: options[:os][:password],
transport_options: { ssl: { verify: false } },
)

processor = options[:class].new(client: client)
processor = options[:class].new(client: client, options: options)

if ARGV.empty?
mail = Mail.new($stdin.read)
Expand Down
6 changes: 3 additions & 3 deletions lib/email_report_processor/dmarc_rua.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module EmailReportProcessor
class DmarcRua < Base
DEFAULT_INDEX = 'dmarc-reports'

def initialize(**options)
@index_name = options[:dmarc_endpoint] || DEFAULT_INDEX
super(client: options[:client])
def initialize(client:, options: {})
@index_name = options[:dmarc_index] || DEFAULT_INDEX
super(client: client)
end

def report(raw_report)
Expand Down
6 changes: 3 additions & 3 deletions lib/email_report_processor/tlsrpt_rua.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module EmailReportProcessor
class TlsrptRua < Base
DEFAULT_INDEX = 'tlsrpt-reports'

def initialize(**options)
@index_name = options[:tlsrpt_endpoint] || DEFAULT_INDEX
super(client: options[:client])
def initialize(client:, options: {})
@index_name = options[:tlsrpt_index] || DEFAULT_INDEX
super(client: client)
end

def report(raw_report)
Expand Down

0 comments on commit 41a8fbf

Please sign in to comment.