Skip to content

Commit

Permalink
stdlib::parsehocon: Support reading files
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Jul 28, 2024
1 parent 99aebdd commit aec6457
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/puppet/functions/stdlib/parsehocon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
#
Expand All @@ -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
Expand Down

0 comments on commit aec6457

Please sign in to comment.