aboutsummaryrefslogtreecommitdiff
path: root/ag
diff options
context:
space:
mode:
Diffstat (limited to 'ag')
-rwxr-xr-xag72
1 files changed, 35 insertions, 37 deletions
diff --git a/ag b/ag
index d54cabe..9d5c661 100755
--- a/ag
+++ b/ag
@@ -2,7 +2,7 @@
# Ag -- archiving all the 'golden' flamewars on -dev
# Alex Legler <a3li@gentoo.org>
-$VERBOSE=nil
+$VERBOSE = nil
require 'bundler/setup'
require 'mail'
@@ -31,70 +31,70 @@ $options.argmode = nil
$options.comment = nil
op = OptionParser.new do |opts|
- actions = %w(hide-msg unhide-msg index-full index-new delete-msg delete-index reindex rethread info).map { |s| '--'+s }.join('|')
+ actions = %w(hide-msg unhide-msg index-full index-new delete-msg delete-index reindex rethread info).map { |s| '--' + s }.join('|')
opts.banner = "Usage: ag <<#{actions}>> <--list listname>> <[--file|--msgid|--hash] <maildir/file/hash/messageid>> [options]"
opts.on('--index-full', 'Read the full past archive from Maildir/cur. Does --delete-index by default. Needs --list and a Maildir') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_full
$options.argmode = :dir
end
opts.on('--index-new', 'Read new messages from Maildir/new and move them to Maildir/cur. Needs --list and a Maildir') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_incremental
$options.argmode = :dir
end
opts.on('--delete-msg', 'Delete message. Needs --list and one of --file, --msgid, or --hash') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_delete_msg
end
opts.on('--hide-msg', 'Hides a message. Needs --list and one of --file, --msgid, or --hash') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_hide_msg
end
opts.on('--unhide-msg', 'Unhides a message. Needs --list and one of --file, --msgid, or --hash') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_unhide_msg
end
opts.on('--create-index', 'Create index but do not populate. Needs --list') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_create_index
$options.need_argument = false
end
opts.on('--rethread', 'Rethread messages. Needs --list') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_rethread
$options.need_argument = false
end
opts.on('--delete-index', 'Delete index. Needs --list') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_delete_index
$options.need_argument = false
end
opts.on('--info', 'Display message details. Needs --list and one of --file, --msgid, or --hash') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_info
end
opts.on('--reindex', 'Reindex message. Needs --list and --file') do
- abort 'Can only select one action' if $options.action != nil
+ abort 'Can only select one action' unless $options.action.nil?
$options.action = :do_reindex
end
@@ -154,9 +154,12 @@ op.parse!
abort op.help unless $options.action
abort 'List name required' unless $options.name
-$options.dir = ARGV[0] or abort 'Need a Maildir/File/Hash/Message-Id to work with' if $options.need_argument
+if $options.need_argument
+ abort 'Need a Maildir/File/Hash/Message-Id to work with' if ARGV.empty?
+ $options.dir = ARGV[0]
+end
-if($options.argmode == :dir)
+if $options.argmode == :dir
# Open maildir and set serializer
$maildir = Maildir.new(File.join($options.dir), false)
$maildir.serializer = Maildir::Serializer::Mail.new
@@ -171,14 +174,14 @@ Ag::Utils.proc_count = $options.jobs
###############################################################################
def do_full
- abort "Wrong argument type: #{$options.argmode.to_s}" unless $options.argmode == :dir
+ abort "Wrong argument type: #{$options.argmode}" unless $options.argmode == :dir
do_delete_index(ignore_missing: true, _raise: true) unless $options.readonly
do_create_index(ignore_exists: true, _raise: true)
messages = $maildir.list(:cur)
opts = {
- :in_processes => Ag::Utils.proc_count,
+ in_processes: Ag::Utils.proc_count
}
opts[:progress] = "Importing #{$options.name}" if $options.progress
Parallel.each(messages, opts) do |maildir_message|
@@ -196,12 +199,12 @@ def do_full
end
def do_incremental
- abort "Wrong argument type: #{$options.argmode.to_s}" unless $options.argmode == :dir
+ abort "Wrong argument type: #{$options.argmode}" unless $options.argmode == :dir
messages = $maildir.list(:new)
do_create_index(ignore_exists: true, _raise: true)
opts = {
- :in_processes => Ag::Utils.proc_count,
+ in_processes: Ag::Utils.proc_count
}
opts[:progress] = "Importing #{$options.name}" if $options.progress
Parallel.each(messages, opts) do |maildir_message|
@@ -254,27 +257,23 @@ def do_unhide_msg
end
def do_delete_index(ignore_missing: false, _raise: false)
- begin
- Ag::Storage.delete_index($options.name)
- rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
- unless ignore_missing
- raise e if _raise
- $stderr.puts "Index does not exist: #{e}"
- end
- rescue => e
+ Ag::Storage.delete_index($options.name)
+rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
+ unless ignore_missing
raise e if _raise
- $stderr.puts "Cannot delete index: #{e}"
+ $stderr.puts "Index does not exist: #{e}"
end
+rescue => e
+ raise e if _raise
+ $stderr.puts "Cannot delete index: #{e}"
end
def do_create_index(ignore_exists: false, _raise: false)
- begin
- Ag::Storage.create_index($options.name)
- rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
- unless (ignore_exists and e.message =~ /IndexAlreadyExistsException/)
- raise e if _raise
- $stderr.puts "Cannot create index #{e}"
- end
+ Ag::Storage.create_index($options.name)
+rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
+ unless ignore_exists && e.message =~ /IndexAlreadyExistsException/
+ raise e if _raise
+ $stderr.puts "Cannot create index #{e}"
end
end
@@ -293,8 +292,7 @@ def do_info
require 'pp'
str = "Message #{id}"
- $stderr.puts str
- $stderr.puts '-' * str.length
+ $stderr.puts str, '-' * str.length
pp message['_source']
rescue => e
@@ -304,7 +302,7 @@ end
###############################################################################
-if self.private_methods.include? $options.action
+if private_methods.include? $options.action
send $options.action
else
abort "Internal Error: Unknown action: #{$options.action}"