]> gitweb.michael.orlitzky.com - mailshears.git/blobdiff - lib/rm/rm_runner.rb
Factor out the description-message-building in the runners.
[mailshears.git] / lib / rm / rm_runner.rb
index 039ae61f7d489c9a0a7807608b959fd912f3c99c..1c2d344475e27d3f763553d15dfbbfb37fbe4d40 100644 (file)
@@ -1,32 +1,51 @@
+require 'common/errors'
+require 'common/runner'
+
+# Perform the removal of users/domains using {RmPlugin}s.
+#
 class RmRunner
   include Runner
 
-  def run(plugin, targets)
+  # Run *plugin* to remove the users/domains in *targets*. The method
+  # signature includes the unused *cfg* for consistency with the
+  # runners that do need a {Configuration}.
+  #
+  # @param cfg [Configuration] unused.
+  #
+  # @param plugin [Class] plugin class that will perform the removal.
+  #
+  # @param targets [Array<User,Domain>] the users and domains to be
+  #   removed.
+  #
+  def run(cfg, plugin, *targets)
     targets.each do |target|
-      # Why think too hard? An account has an @, a domain doesn't.
-      if target.include?('@') then
-        begin
-          account_description = plugin.describe_account(target)
-          plugin.delete_account(target)
-          puts "Removed account: #{target} (#{account_description})"
-        rescue StandardError => e
-          puts "There was an error removing the account: #{e.to_s}"
-          Kernel.exit(ExitCodes::DATABASE_ERROR)
-        end
-      else
-        begin
-          # TODO: Delete all accounts first.
-          domain_description = plugin.describe_domain(target)
-          plugin.delete_domain(target)
-          puts "Removed domain: #{target} (#{domain_description})"
-        rescue StandardError => e
-          puts "There was an error removing the domain: #{e.to_s}"
-          Kernel.exit(ExitCodes::DATABASE_ERROR)
-        end
-      end
+      remove_target(plugin, target)
     end
-
-    # TODO: remove from postfixadmin as well.
   end
 
+
+  protected;
+
+  # Remove *target* using *plugin*. This operation is separate from
+  # the <tt>run()</tt> method so that it can be accessed by the prune
+  # runner.
+  #
+  # @param plugin [RmPlugin] the plugin that will remove the *target*.
+  #
+  # @param target [User,Domain] the user or domain to remove.
+  #
+  def remove_target(plugin, target)
+    target_description = plugin.describe(target)
+
+    begin
+      plugin.remove(target)
+      msg =  "Removed #{target.class.to_s().downcase()} "
+      msg += add_description(target, target_description)
+      msg += '.'
+
+      report(plugin, msg)
+    rescue NonexistentDomainError, NonexistentUserError => e
+      report(plugin, "#{target.class.to_s()} #{e.to_s} not found.")
+    end
+  end
 end