Skip to content

Commit aec6457

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

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/puppet/functions/stdlib/parsehocon.rb

+12-4
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,11 +21,15 @@
1721
end
1822

1923
def parsehocon(hocon_string, default = :no_default_provided)
20-
require 'hocon/config_factory'
21-
2224
begin
23-
data = Hocon::ConfigFactory.parse_string(hocon_string)
24-
data.resolve.root.unwrapped
25+
if File.exist? hocon_string
26+
require 'hocon'
27+
Hocon.load(hocon_string)
28+
else
29+
require 'hocon/config_factory'
30+
data = Hocon::ConfigFactory.parse_string(hocon_string)
31+
data.resolve.root.unwrapped
32+
end
2533
rescue Hocon::ConfigError::ConfigParseError => e
2634
Puppet.debug("Parsing hocon failed with error: #{e.message}")
2735
raise e if default == :no_default_provided

0 commit comments

Comments
 (0)