From 0f9f0e63e0177362db7c11006f70e630404d7092 Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sun, 28 Jul 2024 14:14:39 +0200 Subject: [PATCH 1/2] stdlib::parsehocon: Support reading files --- lib/puppet/functions/stdlib/parsehocon.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/puppet/functions/stdlib/parsehocon.rb b/lib/puppet/functions/stdlib/parsehocon.rb index 159028f99..84d48ef12 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,16 +21,18 @@ end def parsehocon(hocon_string, default = :no_default_provided) - require 'hocon/config_factory' - - begin + 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 - rescue Hocon::ConfigError::ConfigParseError => e - Puppet.debug("Parsing hocon failed with error: #{e.message}") - raise e if default == :no_default_provided - - default end + rescue Hocon::ConfigError::ConfigParseError => e + Puppet.debug("Parsing hocon failed with error: #{e.message}") + raise e if default == :no_default_provided + + default end end From a0c4b9eb83fc9abc2f88cde0c243018c5dd8abfc Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sun, 28 Jul 2024 14:15:15 +0200 Subject: [PATCH 2/2] Add .vendor to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3f1551212..766ada3a7 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ /spec/fixtures/modules/* /tmp/ /vendor/ +/.vendor/ /convert_report.txt /update_report.txt .DS_Store