Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdlib::parsehocon: Support reading files #1436

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/spec/fixtures/modules/*
/tmp/
/vendor/
/.vendor/
/convert_report.txt
/update_report.txt
.DS_Store
Expand Down
22 changes: 14 additions & 8 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,16 +21,18 @@
end

def parsehocon(hocon_string, default = :no_default_provided)
require 'hocon/config_factory'

begin
if File.exist? hocon_string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this, because this can also run on the server. It becomes unpredictable.

Isn't there a way to read a file in puppet? I know file() can be used in some cases. Its docs mention it can also read absolute files, so I'd expect this to work:

stdlib::parsehocon(file('/path/to/file'))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes the idea was to read a file from a PE installation on the puppetserver. Your solution should work. I think I will propose this function to the hocon module in case that's still maintained.

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
Loading