commit:     ef43e934de266b49dec9373cb6c281c0ee7c67dc
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Tue Feb 24 02:16:55 2015 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Tue Feb 24 02:16:55 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/ag.git;a=commit;h=ef43e934

Improve delete code paths.

Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>

---
 ag             | 37 +++++++++++++++++++++++--------------
 lib/storage.rb |  4 ++++
 lib/utils.rb   |  6 ++++++
 3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/ag b/ag
index 57dc0a4..ffe714b 100755
--- a/ag
+++ b/ag
@@ -26,41 +26,46 @@ $options.debug = false
 $options.readonly = false
 $options.jobs = false
 $options.progress = true
+$options.need_argument = true
+$options.argmode = nil
 
 op = OptionParser.new do |opts|
   opts.banner = "Usage: ag 
<<--index-full|--index-new|--delete-msg|--delete-index|--reindex|--info> 
<--list listname>> <[--file|--msgid|--hash] <maildir/file/hash/messageid>> 
[options]"
 
-  opts.on('--index-full', 'Read the full past archive from the .cur Maildir') 
do
+  opts.on('--index-full', 'Read the full past archive from Maildir/cur. Needs 
--list and a Maildir') do
     abort 'Can only select one action' if $options.action != nil
 
     $options.action = :do_full
+    $options.argmode = :dir
   end
 
-  opts.on('--index-new', 'Read new messages from .new and move them to .cur') 
do
+  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
 
     $options.action = :do_incremental
+    $options.argmode = :dir
   end
 
-  opts.on('--delete-msg', 'Delete message. Needs --file, --msgid, or --hash') 
do
+  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
 
     $options.action = :do_delete_msg
   end
-  
-  opts.on('--delete-Index', 'Delete index. Needs --list') do
+
+  opts.on('--delete-index', 'Delete index. Needs --list') do
     abort 'Can only select one action' if $options.action != nil
 
     $options.action = :do_index
+    $options.need_argument = false
   end
 
-  opts.on('--info', 'Display message details. Needs --file, --msgid, or 
--hash') do
+  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
 
     $options.action = :do_info
   end
 
-  opts.on('--reindex', 'Reindex message. Needs --file') do
+  opts.on('--reindex', 'Reindex message. Needs --list and --file') do
     abort 'Can only select one action' if $options.action != nil
 
     $options.action = :do_reindex
@@ -105,7 +110,7 @@ op = OptionParser.new do |opts|
   opts.on('--jobs JOBS', 'Number of parallel jobs to run (defaults to 75% of 
core count)') do |jobs|
     $options.jobs = jobs.to_i
   end
-  
+
   opts.on('--progress', 'Display the progress bar') do
     $options.progress = true
   end
@@ -117,11 +122,13 @@ 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'
+$options.dir = ARGV[0] or abort 'Need a Maildir/File/Hash/Message-Id to work 
with' if $options.need_argument
 
-# Open maildir and set serializer
-$maildir = Maildir.new(File.join($options.dir), false)
-$maildir.serializer = Maildir::Serializer::Mail.new
+if($options.argmode == :dir)
+  # Open maildir and set serializer
+  $maildir = Maildir.new(File.join($options.dir), false)
+  $maildir.serializer = Maildir::Serializer::Mail.new
+end
 
 # Connect to Elasticsearch
 $es = Elasticsearch::Client.new(log: false)
@@ -132,12 +139,13 @@ Ag::Utils.proc_count = $options.jobs
 ###############################################################################
 
 def do_full
+  abort "Wrong argument type: #{$options.argmode.to_s}" unless 
$options.argmode == :dir
   Ag::Storage.create_index($options.name)
 
   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|
@@ -155,10 +163,11 @@ def do_full
 end
 
 def do_incremental
+  abort "Wrong argument type: #{$options.argmode.to_s}" unless 
$options.argmode == :dir
   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|

diff --git a/lib/storage.rb b/lib/storage.rb
index 8ab2d4e..68de268 100644
--- a/lib/storage.rb
+++ b/lib/storage.rb
@@ -141,6 +141,10 @@ module Ag::Storage
     resolve_by_field(list, :raw_filename, filename)
   end
 
+  def resolve_hash(list, hash = nil)
+    resolve_by_field(list, :_id, hash)
+  end
+
   def store(list, message, filename)
     content = get_content(message, filename)
 

diff --git a/lib/utils.rb b/lib/utils.rb
index 38349e0..b9156cd 100644
--- a/lib/utils.rb
+++ b/lib/utils.rb
@@ -33,6 +33,12 @@ module Ag
         id = Ag::Storage.resolve_message_id($options.name, $options.dir)
       when :file
         id = Ag::Storage.resolve_filename($options.name, $options.dir)
+      when :hash
+        id = Ag::Storage.resolve_hash($options.name, $options.dir)
+      when :dir
+        abort 'Cannot perform resolve a directory to a message id'
+      when nil
+        abort 'Cannot resolve a nil id'
       end
 
       id

Reply via email to