Skip to content

Instantly share code, notes, and snippets.

@antaflos
Created October 11, 2018 13:31
Show Gist options
  • Save antaflos/684f1126ae0fa9375f77cf6af822fbf3 to your computer and use it in GitHub Desktop.
Save antaflos/684f1126ae0fa9375f77cf6af822fbf3 to your computer and use it in GitHub Desktop.
Puppet::Functions.create_function(:file_lookup_key) do
dispatch :file_lookup_key do
param 'String[1]', :key
param 'Hash[String[1],Any]', :options
param 'Puppet::LookupContext', :context
end
def file_lookup_key(key, options, context)
# Set interpolate = true by default and override it with the value of
# options['interpolate'], if set.
interpolate = true
interpolate = options['interpolate'] unless options['interpolate'].nil?
context.explain { "Setting interpolate = #{interpolate}" }
return context.cached_value(key) if context.cache_has_key(key)
data_file_path = File.join(options['path'], key)
data = nil
context.explain { "Looking for #{key} at #{data_file_path}" }
begin
raw_data = context.cached_file_data(data_file_path)
if interpolate == true
data = context.interpolate(raw_data)
else
data = raw_data
end
context.explain { "Found #{key} at #{data_file_path}" }
rescue Errno::ENOENT
context.not_found
end
context.cache(key, data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment