diff --git a/lib/puppet/parser/functions/ensure_prefix.rb b/lib/puppet/parser/functions/ensure_prefix.rb index 30592284..58bf4135 100644 --- a/lib/puppet/parser/functions/ensure_prefix.rb +++ b/lib/puppet/parser/functions/ensure_prefix.rb @@ -19,14 +19,13 @@ module Puppet::Parser::Functions Will return: ['p.a', 'p.b', 'p.c'] EOS - ) do |arguments| - - raise(Puppet::ParseError, 'ensure_prefix(): Wrong number of arguments ' + + ) do |arguments| + raise(Puppet::ParseError, 'ensure_prefix(): Wrong number of arguments ' \ "given (#{arguments.size} for 2)") if arguments.size < 2 enumerable = arguments[0] - unless enumerable.is_a?(Array) or enumerable.is_a?(Hash) + unless enumerable.is_a?(Array) || enumerable.is_a?(Hash) raise Puppet::ParseError, "ensure_prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}" end @@ -38,18 +37,18 @@ module Puppet::Parser::Functions end end - if enumerable.is_a?(Array) - # Turn everything into string same as join would do ... - result = enumerable.collect do |i| - i = i.to_s - prefix && !i.start_with?(prefix) ? prefix + i : i - end - else - result = Hash[enumerable.map do |k,v| - k = k.to_s - [ prefix && !k.start_with?(prefix) ? prefix + k : k, v ] - end] - end + result = if enumerable.is_a?(Array) + # Turn everything into string same as join would do ... + enumerable.map do |i| + i = i.to_s + prefix && !i.start_with?(prefix) ? prefix + i : i + end + else + Hash[enumerable.map do |k, v| + k = k.to_s + [prefix && !k.start_with?(prefix) ? prefix + k : k, v] + end] + end return result end diff --git a/lib/puppet/parser/functions/to_hash_settings.rb b/lib/puppet/parser/functions/to_hash_settings.rb index 70975ec7..576180c8 100644 --- a/lib/puppet/parser/functions/to_hash_settings.rb +++ b/lib/puppet/parser/functions/to_hash_settings.rb @@ -24,8 +24,7 @@ module Puppet::Parser::Functions 'foo: b' => {'key' => 'b', 'value' => 2} } EOS - ) do |arguments| - + ) do |arguments| hash, id = arguments id = (id.nil? ? '' : "#{id}: ") diff --git a/lib/puppet/provider/package/pear.rb b/lib/puppet/provider/package/pear.rb index 690f069c..8150db24 100644 --- a/lib/puppet/provider/package/pear.rb +++ b/lib/puppet/provider/package/pear.rb @@ -21,8 +21,8 @@ def self.pearlist(hash) begin list = execute(command).split("\n") - list = list.collect do |set| - if match = %r{INSTALLED PACKAGES, CHANNEL (.*):}i.match(set) + list = list.map do |set| + if match = %r{INSTALLED PACKAGES, CHANNEL (.*):}i.match(set) # rubocop:disable Lint/AssignmentInCondition channel = match[1].downcase end @@ -31,76 +31,69 @@ def self.pearlist(hash) pearhash = pearsplit(set, channel) pearhash[:provider] = :pear pearhash - else - nil end else - if pearhash = pearsplit(set, channel) + if pearhash = pearsplit(set, channel) # rubocop:disable Lint/AssignmentInCondition pearhash[:provider] = :pear pearhash - else - nil end end end.reject { |p| p.nil? } rescue Puppet::ExecutionFailure => detail - raise Puppet::Error, 'Could not list pears: %s' % detail + raise Puppet::Error, format('Could not list pears: %s', detail) end - if hash[:justme] - return list.shift - else - return list - end + return list.shift if hash[:justme] + list end def self.pearsplit(desc, channel) desc.strip! case desc - when %r{^$} then return nil - when %r{^INSTALLED}i then return nil - when %r{no packages installed}i then return nil - when %r{^=} then return nil - when %r{^PACKAGE}i then return nil - when %r{^(\S+)\s+(\S+)\s+(\S+)\s*$} then - name = $1 - version = $2 - state = $3 - return { - name: "#{channel}/#{name}", - ensure: state == 'stable' ? version : state - } + when %r{^$} then return nil + when %r{^INSTALLED}i then return nil + when %r{no packages installed}i then return nil + when %r{^=} then return nil + when %r{^PACKAGE}i then return nil + when %r{^(\S+)\s+(\S+)\s+(\S+)\s*$} then + name = Regexp.last_match(1) + version = Regexp.last_match(2) + state = Regexp.last_match(3) + return { + name: "#{channel}/#{name}", + ensure: state == 'stable' ? version : state + } else - Puppet.debug "Could not match '%s'" % desc + Puppet.debug format("Could not match '%s'", desc) nil end end def self.instances - pearlist(local: true).collect do |hash| + pearlist(local: true).map do |hash| new(hash) end end def install(useversion = true) command = ['-D', 'auto_discover=1', 'upgrade'] - if @resource[:install_options] - command << @resource[:install_options] - else - command << '--alldeps' - end - - if source = @resource[:source] - command << source - else - if (!@resource.should(:ensure).is_a? Symbol) and useversion - command << "#{@resource[:name]}-#{@resource.should(:ensure)}" - else - command << @resource[:name] - end - end + command << if @resource[:install_options] + @resource[:install_options] + else + '--alldeps' + end + + command << if @resource[:source] + @resource[:source] + else + if (!@resource.should(:ensure).is_a? Symbol) && useversion + "#{@resource[:name]}-#{@resource.should(:ensure)}" + else + @resource[:name] + end + end pearcmd(*command) end @@ -109,12 +102,10 @@ def latest # This always gets the latest version available. version = '' command = [command(:pearcmd), 'remote-info', @resource[:name]] - list = execute(command).split("\n") - list = list.collect do |set| - if set =~ %r{^Latest} - version = set.split[1] - end + execute(command).each_line do |set| + version = set.split[1] if set =~ %r{^Latest} end + version end @@ -124,14 +115,10 @@ def query def uninstall output = pearcmd 'uninstall', @resource[:name] - if output =~ %r{^uninstall ok} - else - raise Puppet::Error, output - end + raise Puppet::Error, output unless output =~ %r{^uninstall ok} end def update install(false) end - end diff --git a/lib/puppet/provider/package/pecl.rb b/lib/puppet/provider/package/pecl.rb index 66035d65..bb4de994 100644 --- a/lib/puppet/provider/package/pecl.rb +++ b/lib/puppet/provider/package/pecl.rb @@ -8,46 +8,37 @@ has_feature :upgradeable case Facter.value(:operatingsystem) - when 'Solaris' - commands peclcmd: '/opt/coolstack/php5/bin/pecl' - else - commands peclcmd: 'pecl' + when 'Solaris' + commands peclcmd: '/opt/coolstack/php5/bin/pecl' + else + commands peclcmd: 'pecl' end def self.pecllist(hash) command = [command(:peclcmd), 'list'] begin - list = execute(command).split("\n").collect do |set| + list = execute(command).split("\n").map do |set| if hash[:justme] if %r{^#{hash[:justme]}$}i.match(set) - if peclhash = peclsplit(set) + if peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition peclhash[:provider] = :peclcmd peclhash - else - nil end - else - nil end else - if peclhash = peclsplit(set) + if peclhash = peclsplit(set) # rubocop:disable Lint/AssignmentInCondition peclhash[:provider] = :peclcmd peclhash - else - nil end end end.reject { |p| p.nil? } rescue Puppet::ExecutionFailure => detail - raise Puppet::Error, 'Could not list pecls: %s' % detail + raise Puppet::Error, format('Could not list pecls: %s', detail) end - if hash[:justme] - return list.shift - else - return list - end + return list.shift if hash[:justme] + list end def self.peclsplit(desc) @@ -58,23 +49,23 @@ def self.peclsplit(desc) when %r{No packages installed from channel}i then return nil when %r{^=} then return nil when %r{^PACKAGE} then return nil - when %r{\[1m} then return nil # Newer versions of PEAR use colorized output + when %r{\[1m} then return nil # Newer versions of PEAR use colorized output when %r{^(\S+)\s+(\S+)\s+\S+} then - name = $1 - version = $2 + name = Regexp.last_match(1) + version = Regexp.last_match(2) return { name: "pecl-#{name.downcase}", ensure: version } else - Puppet.warning 'Could not match %s' % desc + Puppet.warning format('Could not match %s', desc) nil end end def self.instances - pecllist(local: true).collect do |hash| + pecllist(local: true).map do |hash| new(hash) end end @@ -86,10 +77,10 @@ def peclname def install(useversion = true) command = ['upgrade'] - if source = @resource[:source] - command << source + if @resource[:source] + command << @resource[:source] else - if (!@resource.should(:ensure).is_a? Symbol) and useversion + if (!@resource.should(:ensure).is_a? Symbol) && useversion command << '-f' command << "#{peclname}-#{@resource.should(:ensure)}" else @@ -97,9 +88,9 @@ def install(useversion = true) end end - if pipe = @resource[:pipe] - command << '<<<' - command << @resource[:pipe] + if pipe == @resource[:pipe] + command << '<<<' + command << @resource[:pipe] end peclcmd(*command) @@ -108,10 +99,8 @@ def install(useversion = true) def latest version = '' command = [command(:peclcmd), 'remote-info', peclname] - list = execute(command).each_line do |set| - if set =~ %r{^Latest} - version = set.split[1] - end + execute(command).each_line do |set| + version = set.split[1] if set =~ %r{^Latest} end version @@ -123,10 +112,7 @@ def query def uninstall output = peclcmd 'uninstall', peclname - if output =~ %r{^uninstall ok} - else - raise Puppet::Error, output - end + raise Puppet::Error, output unless output =~ %r{^uninstall ok} end def update