Skip to content

Commit 0f9f0e6

Browse files
committed
stdlib::parsehocon: Support reading files
1 parent 99aebdd commit 0f9f0e6

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/puppet/functions/stdlib/parsehocon.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# This function accepts HOCON as a string and converts it into the correct
55
# Puppet structure
66
#
7+
# @param hocon_string can be an actual string of data or a path to a Hocon config file
8+
#
9+
# @param default content that will be returned in case the string isn't parseable
10+
#
711
# @example How to parse hocon
812
# $data = stdlib::parsehocon("{any valid hocon: string}")
913
#
@@ -17,16 +21,18 @@
1721
end
1822

1923
def parsehocon(hocon_string, default = :no_default_provided)
20-
require 'hocon/config_factory'
21-
22-
begin
24+
if File.exist? hocon_string
25+
require 'hocon'
26+
Hocon.load(hocon_string)
27+
else
28+
require 'hocon/config_factory'
2329
data = Hocon::ConfigFactory.parse_string(hocon_string)
2430
data.resolve.root.unwrapped
25-
rescue Hocon::ConfigError::ConfigParseError => e
26-
Puppet.debug("Parsing hocon failed with error: #{e.message}")
27-
raise e if default == :no_default_provided
28-
29-
default
3031
end
32+
rescue Hocon::ConfigError::ConfigParseError => e
33+
Puppet.debug("Parsing hocon failed with error: #{e.message}")
34+
raise e if default == :no_default_provided
35+
36+
default
3137
end
3238
end

0 commit comments

Comments
 (0)