From aec64577f3eefae78dd2c1b88436e5b9ed6751b8 Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sun, 28 Jul 2024 14:14:39 +0200 Subject: [PATCH] stdlib::parsehocon: Support reading files --- lib/puppet/functions/stdlib/parsehocon.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/puppet/functions/stdlib/parsehocon.rb b/lib/puppet/functions/stdlib/parsehocon.rb index 159028f99..747f79204 100644 --- a/lib/puppet/functions/stdlib/parsehocon.rb +++ b/lib/puppet/functions/stdlib/parsehocon.rb @@ -4,6 +4,10 @@ # This function accepts HOCON as a string and converts it into the correct # Puppet structure # +# @param hocon_string can be an actual string of data or a path to a Hocon config file +# +# @param default content that will be returned in case the string isn't parseable +# # @example How to parse hocon # $data = stdlib::parsehocon("{any valid hocon: string}") # @@ -17,11 +21,15 @@ end def parsehocon(hocon_string, default = :no_default_provided) - require 'hocon/config_factory' - begin - data = Hocon::ConfigFactory.parse_string(hocon_string) - data.resolve.root.unwrapped + if File.exist? hocon_string + require 'hocon' + Hocon.load(hocon_string) + else + require 'hocon/config_factory' + data = Hocon::ConfigFactory.parse_string(hocon_string) + data.resolve.root.unwrapped + end rescue Hocon::ConfigError::ConfigParseError => e Puppet.debug("Parsing hocon failed with error: #{e.message}") raise e if default == :no_default_provided