-
Notifications
You must be signed in to change notification settings - Fork 580
add hash2ini function #620
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Puppet::Parser::Functions | ||
newfunction(:hash2ini, :type => :rvalue, :doc => <<-EOS | ||
This converts a puppet hash to an INI string. | ||
EOS | ||
) do |arguments| | ||
|
||
if arguments.size < 1 | ||
raise(Puppet::ParseError, 'hash2ini(): requires at least one argument') | ||
end | ||
if arguments.size >= 3 | ||
raise(Puppet::ParseError, 'hash2ini(): too many arguments') | ||
end | ||
unless arguments[0].is_a?(Hash) | ||
raise(Puppet::ParseError, 'hash2ini(): requires a hash as argument') | ||
end | ||
|
||
h = arguments[0] | ||
|
||
settings = { | ||
'header' => '# THIS FILE IS CONTROLLED BY PUPPET', | ||
'section_prefix' => '[', | ||
'section_suffix' => ']', | ||
'key_val_separator' => '=', | ||
'quote_char' => '"', | ||
} | ||
|
||
if arguments[1] | ||
unless arguments[1].is_a?(Hash) | ||
raise(Puppet::ParseError, 'hash2ini(): Requires a hash as argument') | ||
end | ||
settings.merge!(arguments[1]) | ||
end | ||
|
||
|
||
|
||
ini = [] | ||
ini << settings['header'] << nil | ||
h.keys.each do |section| | ||
ini << "#{settings['section_prefix']}#{section}#{settings['section_suffix']}" | ||
h[section].each do |k, v| | ||
ini << "#{k}#{settings['key_val_separator']}#{settings['quote_char']}#{v}#{settings['quote_char']}" | ||
end | ||
ini << nil | ||
end | ||
return ini.join("\n") | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'spec_helper' | ||
|
||
describe 'hash2ini' do | ||
it { is_expected.not_to eq(nil) } | ||
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /requires at least one argument/) } | ||
it { is_expected.to run.with_params({}, {}, {}).and_raise_error(Puppet::ParseError, /too many arguments/) } | ||
it { is_expected.to run.with_params('some string').and_raise_error(Puppet::ParseError, /requires a hash as argument/) } | ||
|
||
example_input = { | ||
'main' => { | ||
'logging' => 'INFO', | ||
'limit' => 314, | ||
'awesome' => true, | ||
}, | ||
'dev' => { | ||
'logging' => 'DEBUG', | ||
'log_location' => '/var/log/dev.log', | ||
} | ||
} | ||
|
||
context 'no custom settings' do | ||
output=<<-EOS | ||
# THIS FILE IS CONTROLLED BY PUPPET | ||
|
||
[main] | ||
logging="INFO" | ||
limit="314" | ||
awesome="true" | ||
|
||
[dev] | ||
logging="DEBUG" | ||
log_location="/var/log/dev.log" | ||
EOS | ||
it { is_expected.to run.with_params(example_input).and_return(output) } | ||
end | ||
|
||
context 'custom settings' do | ||
settings = { | ||
'header' => '; THIS FILE IS CONTROLLED BY /dev/random', | ||
'section_prefix' => '[[', | ||
'section_suffix' => ']]', | ||
'key_val_separator' => ': ', | ||
'quote_char' => "", | ||
} | ||
output=<<-EOS | ||
; THIS FILE IS CONTROLLED BY /dev/random | ||
|
||
[[main]] | ||
logging: INFO | ||
limit: 314 | ||
awesome: true | ||
|
||
[[dev]] | ||
logging: DEBUG | ||
log_location: /var/log/dev.log | ||
EOS | ||
it { is_expected.to run.with_params(example_input, settings).and_return(output) } | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will behave strangely if
v
is not a string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true. I can document that a string is expected for the value, all merging of of arrays of values must be done beforehand. There's no real standard on arrays of values for ini files so I'd just leave it up to the programmer to do what is necessary rather than put the logic in hash2ini().