summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-07-09 18:06:37 -0700
committerMarkus Roberts <Markus@reality.com>2010-07-09 18:06:37 -0700
commit07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b (patch)
treef4f20be112f0c8832cb2e9681935a04bb0d197cf /lib/puppet/util
parent8d1fbe4586c91682cdda0cb271649e918fd9778b (diff)
downloadpuppet-07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b.tar.gz
puppet-07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b.tar.xz
puppet-07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b.zip
Code smell: Avoid unneeded blocks
Replaced 45 occurances of (DEF) begin (LINES) rescue(.*) (LINES) end end with 3 Examples: The code: def find(name) begin self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end end becomes: def find(name) self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end The code: def exit_on_fail(message, code = 1) begin yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end end becomes: def exit_on_fail(message, code = 1) yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end The code: def start begin case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end end becomes: def start case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end
Diffstat (limited to 'lib/puppet/util')
-rw-r--r--lib/puppet/util/autoload/file_cache.rb4
-rwxr-xr-xlib/puppet/util/filetype.rb16
-rw-r--r--lib/puppet/util/ldap/connection.rb4
-rw-r--r--lib/puppet/util/rdoc.rb4
-rw-r--r--lib/puppet/util/rdoc/generators/puppet_generator.rb8
5 files changed, 9 insertions, 27 deletions
diff --git a/lib/puppet/util/autoload/file_cache.rb b/lib/puppet/util/autoload/file_cache.rb
index 7303c9a1c..ce30eed73 100644
--- a/lib/puppet/util/autoload/file_cache.rb
+++ b/lib/puppet/util/autoload/file_cache.rb
@@ -83,12 +83,10 @@ module Puppet::Util::Autoload::FileCache
end
def protect(path)
- begin
yield
- rescue => detail
+ rescue => detail
raise unless detail.class.to_s.include?("Errno")
missing_file(path)
return false
- end
end
end
diff --git a/lib/puppet/util/filetype.rb b/lib/puppet/util/filetype.rb
index 4d7da67eb..712895f1f 100755
--- a/lib/puppet/util/filetype.rb
+++ b/lib/puppet/util/filetype.rb
@@ -205,23 +205,19 @@ class Puppet::Util::FileType
newfiletype(:suntab) do
# Read a specific @path's cron tab.
def read
- begin
output = Puppet::Util.execute(%w{crontab -l}, :uid => @path)
return "" if output.include?("can't open your crontab")
raise Puppet::Error, "User #{@path} not authorized to use cron" if output.include?("you are not authorized to use cron")
return output
- rescue => detail
+ rescue => detail
raise Puppet::Error, "Could not read crontab for #{@path}: #{detail}"
- end
end
# Remove a specific @path's cron tab.
def remove
- begin
Puppet::Util.execute(%w{crontab -r}, :uid => @path)
- rescue => detail
+ rescue => detail
raise Puppet::Error, "Could not remove crontab for #{@path}: #{detail}"
- end
end
# Overwrite a specific @path's cron tab; must be passed the @path name
@@ -250,22 +246,18 @@ class Puppet::Util::FileType
newfiletype(:aixtab) do
# Read a specific @path's cron tab.
def read
- begin
output = Puppet::Util.execute(%w{crontab -l}, :uid => @path)
raise Puppet::Error, "User #{@path} not authorized to use cron" if output.include?("You are not authorized to use the cron command")
return output
- rescue => detail
+ rescue => detail
raise Puppet::Error, "Could not read crontab for #{@path}: #{detail}"
- end
end
# Remove a specific @path's cron tab.
def remove
- begin
Puppet::Util.execute(%w{crontab -r}, :uid => @path)
- rescue => detail
+ rescue => detail
raise Puppet::Error, "Could not remove crontab for #{@path}: #{detail}"
- end
end
# Overwrite a specific @path's cron tab; must be passed the @path name
diff --git a/lib/puppet/util/ldap/connection.rb b/lib/puppet/util/ldap/connection.rb
index 4f71069ef..624bc6f14 100644
--- a/lib/puppet/util/ldap/connection.rb
+++ b/lib/puppet/util/ldap/connection.rb
@@ -60,7 +60,6 @@ class Puppet::Util::Ldap::Connection
# Start our ldap connection.
def start
- begin
case ssl
when :tls
@connection = LDAP::SSLConn.new(host, port, true)
@@ -72,8 +71,7 @@ class Puppet::Util::Ldap::Connection
@connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
@connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON)
@connection.simple_bind(user, password)
- rescue => detail
+ rescue => detail
raise Puppet::Error, "Could not connect to LDAP: #{detail}"
- end
end
end
diff --git a/lib/puppet/util/rdoc.rb b/lib/puppet/util/rdoc.rb
index 82076534d..ef208caff 100644
--- a/lib/puppet/util/rdoc.rb
+++ b/lib/puppet/util/rdoc.rb
@@ -6,7 +6,6 @@ module Puppet::Util::RDoc
# launch a rdoc documenation process
# with the files/dir passed in +files+
def rdoc(outputdir, files, charset = nil)
- begin
Puppet[:ignoreimport] = true
# then rdoc
@@ -34,9 +33,8 @@ module Puppet::Util::RDoc
# launch the documentation process
r.document(options)
- rescue RDoc::RDocError => e
+ rescue RDoc::RDocError => e
raise Puppet::ParseError.new("RDoc error #{e}")
- end
end
# launch a output to console manifest doc
diff --git a/lib/puppet/util/rdoc/generators/puppet_generator.rb b/lib/puppet/util/rdoc/generators/puppet_generator.rb
index 9d41a71ef..614c8ccec 100644
--- a/lib/puppet/util/rdoc/generators/puppet_generator.rb
+++ b/lib/puppet/util/rdoc/generators/puppet_generator.rb
@@ -52,13 +52,11 @@ module Generators
# loads our own html template file
def load_html_template
- begin
require 'puppet/util/rdoc/generators/template/puppet/puppet'
extend RDoc::Page
- rescue LoadError
+ rescue LoadError
$stderr.puts "Could not find Puppet template '#{template}'"
exit 99
- end
end
def gen_method_index
@@ -171,15 +169,13 @@ module Generators
# generate all the subdirectories, modules, classes and files
def gen_sub_directories
- begin
super
File.makedirs(MODULE_DIR)
File.makedirs(NODE_DIR)
File.makedirs(PLUGIN_DIR)
- rescue
+ rescue
$stderr.puts $ERROR_INFO.message
exit 1
- end
end
# generate the index of modules