X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Ffunctions%2Ffact.rb;h=c963d16650be0753008892a7e6c523ef5c29eb04;hb=30caaa85aed7015ca0d77216bff175eebd917eb7;hp=dfb048b5f61d4acddd8f6c674dfcd5142daa7e95;hpb=6963202b4b62c2816655ac9532521b018fdf83bd;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/functions/fact.rb b/3rdparty/modules/stdlib/lib/puppet/functions/fact.rb index dfb048b5f..c963d1665 100644 --- a/3rdparty/modules/stdlib/lib/puppet/functions/fact.rb +++ b/3rdparty/modules/stdlib/lib/puppet/functions/fact.rb @@ -1,27 +1,33 @@ -# Digs into the facts hash using dot-notation +# @summary +# Digs into the facts hash using dot-notation # -# Example usage: +# Supports the use of dot-notation for referring to structured facts. If a fact requested +# does not exist, returns Undef. # +# @example Example usage: # fact('osfamily') # fact('os.architecture') # -# Array indexing: -# +# @example Array indexing: # fact('mountpoints."/dev".options.1') # -# Fact containing a "." in the name: -# +# @example Fact containing a "." in the name: # fact('vmware."VRA.version"') # Puppet::Functions.create_function(:fact) do + # @param fact_name + # The name of the fact to check + # + # @return + # All information retrieved on the given fact_name dispatch :fact do param 'String', :fact_name end def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') + array_path.map { |string| + string.include?('.') ? %("#{string}") : string + }.join('.') end def fact(fact_name) @@ -30,21 +36,20 @@ Puppet::Functions.create_function(:fact) do # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. - path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } + path = fact_name.scan(%r{([^."]+)|(?:")([^"]+)(?:")}).map { |x| x.compact.first } walked_path = [] path.reduce(facts) do |d, k| return nil if d.nil? || k.nil? - case - when d.is_a?(Array) + if d.is_a?(Array) begin result = d[Integer(k)] - rescue ArgumentError => e + rescue ArgumentError => e # rubocop:disable Lint/UselessAssignment : Causes errors if assigment is removed. Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is an array; cannot index to '#{k}'") result = nil end - when d.is_a?(Hash) + elsif d.is_a?(Hash) result = d[k] else Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is not a collection; cannot walk to '#{k}'")