From ef77e919fa61bb5ba7924d49a171dc3a05410a33 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 8 Nov 2015 13:55:08 -0500 Subject: [PATCH] Factor out the description-message-building in the runners. --- doc/TODO | 2 -- lib/common/runner.rb | 40 +++++++++++++++++++++++++++++++++++++++ lib/mv/mv_dummy_runner.rb | 22 ++++----------------- lib/mv/mv_runner.rb | 22 ++++----------------- lib/rm/rm_dummy_runner.rb | 10 ++-------- lib/rm/rm_runner.rb | 10 ++-------- 6 files changed, 52 insertions(+), 54 deletions(-) diff --git a/doc/TODO b/doc/TODO index bd750c9..b809204 100644 --- a/doc/TODO +++ b/doc/TODO @@ -22,6 +22,4 @@ * Make sure removing a domain updates the aliases table correctly. -* Factor out all of the msg-building in e.g. MvRunner. - * Implement moving of domains. diff --git a/lib/common/runner.rb b/lib/common/runner.rb index 2bb66e0..b6fa2e8 100644 --- a/lib/common/runner.rb +++ b/lib/common/runner.rb @@ -17,6 +17,46 @@ module Runner end + # When we're describing a user or domain, we often want to output + # some additional description of it. But then, sometimes that + # additional description is pointless, like when it's exactly the + # same as the username that we're already outputting. This function + # determines whether or not *desc* is pointless for *target*. + # + # @param target [User,Domain] the user or domain described by *desc*. + # + # @param desc [String] a string description of *target*. + # + # @return [Boolean] true if *desc* is a pointless description of + # *target* and false otherwise. + # + def pointless?(target, desc) + return (desc.nil? or desc.empty? or desc == target.to_s()) + end + + + # If *desc* is not a pointless description of *target*, return the + # string representation of *target* followed by *desc* in + # parentheses. If *desc* is pointless, we return only the string + # representation of *target* + # + # @param target [User,Domain] the user or domain we want to describe + # as a string. + # + # @param desc [String] a string description of *target*. + # + # @return [String] the string representation of *target*, possibly + # followed by the non-pointless description *desc*. + # + def add_description(target, desc) + if pointless?(target, desc) + return target.to_s() + else + return target.to_s() + " (#{desc})" + end + end + + # Report a message from the given *plugin*. All this does is prefix # the message with the plugin name and then print it to stdout. # diff --git a/lib/mv/mv_dummy_runner.rb b/lib/mv/mv_dummy_runner.rb index 9ed655a..f730669 100644 --- a/lib/mv/mv_dummy_runner.rb +++ b/lib/mv/mv_dummy_runner.rb @@ -34,24 +34,10 @@ class MvDummyRunner src_description = plugin.describe(src) dst_description = plugin.describe(dst) - msg = "Would move user #{src} " - - # Only append the extra description if it's useful. - if not src_description.nil? and - not src_description.empty? and - not src_description == src.to_s() then - msg += "(#{src_description}) " - end - - msg += "to #{dst}" - - # Only append the extra description if it's useful. - if not dst_description.nil? and - not dst_description.empty? and - not dst_description == dst.to_s() then - msg += " (#{dst_description})" - end - + msg = "Would move user " + msg += add_description(src, src_description) + msg += " to " + add_description(dst, dst_description) msg += "." report(plugin, msg) end diff --git a/lib/mv/mv_runner.rb b/lib/mv/mv_runner.rb index fe44e5c..1fac09e 100644 --- a/lib/mv/mv_runner.rb +++ b/lib/mv/mv_runner.rb @@ -31,24 +31,10 @@ class MvRunner plugin.mv_user(src, dst) dst_description = plugin.describe(dst) - msg = "Moved user #{src} " - - # Only append the extra description if it's useful. - if not src_description.nil? and - not src_description.empty? and - not src_description == src.to_s() then - msg += "(#{src_description}) " - end - - msg += "to #{dst}" - - # Only append the extra description if it's useful. - if not dst_description.nil? and - not dst_description.empty? and - not dst_description == dst.to_s() then - msg += " (#{dst_description})" - end - + msg = "Moved user " + msg += add_description(src, src_description) + msg += " to " + msg += add_description(dst, dst_description) msg += "." report(plugin, msg) diff --git a/lib/rm/rm_dummy_runner.rb b/lib/rm/rm_dummy_runner.rb index 54a8969..2b1f14d 100644 --- a/lib/rm/rm_dummy_runner.rb +++ b/lib/rm/rm_dummy_runner.rb @@ -27,14 +27,8 @@ class RmDummyRunner def run(cfg, plugin, *targets) targets.each do |target| target_description = plugin.describe(target) - msg = "Would remove #{target.class.to_s().downcase()} #{target}" - - # Only append the extra description if it's useful. - if not target_description.nil? and - not target_description.empty? and - not target_description == target.to_s() then - msg += " (#{target_description})" - end + msg = "Would remove #{target.class.to_s().downcase()} " + msg += add_description(target, target_description) msg += '.' report(plugin, msg) diff --git a/lib/rm/rm_runner.rb b/lib/rm/rm_runner.rb index 4162ddf..1c2d344 100644 --- a/lib/rm/rm_runner.rb +++ b/lib/rm/rm_runner.rb @@ -39,14 +39,8 @@ class RmRunner begin plugin.remove(target) - msg = "Removed #{target.class.to_s().downcase()} #{target}" - - # Only append the extra description if it's useful. - if not target_description.nil? and - not target_description.empty? and - not target_description == target.to_s() then - msg += " (#{target_description})" - end + msg = "Removed #{target.class.to_s().downcase()} " + msg += add_description(target, target_description) msg += '.' report(plugin, msg) -- 2.44.2