Skip to content

Commit b7deae7

Browse files
committed
add extlib::to_ini() function
Based on / copied from: https://github.com/mmckinst/puppet-hash2stuff/blob/master/lib/puppet/parser/functions/hash2ini.rb The original author, @mmckinst, claims to have haulted support for this code and has stated no objection to this function being migrated to extlib: mmckinst/puppet-hash2stuff#19 The original hash2ini() function was rejected from stdlib: puppetlabs/puppetlabs-stdlib#620
1 parent ff5b623 commit b7deae7

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

lib/puppet/functions/extlib/to_ini.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# This converts a puppet hash to an INI string.
5+
#
6+
# Based on https://github.com/mmckinst/puppet-hash2stuff/blob/master/lib/puppet/parser/functions/hash2ini.rb
7+
#
8+
# @example How to output ini to a file
9+
# file { '/tmp/config.ini':
10+
# ensure => file,
11+
# content => extlib::to_ini($myhash),
12+
# }
13+
Puppet::Functions.create_function(:'extlib::to_ini') do
14+
# @param data Data structure which needs to be converted into ini
15+
# @param settings Override default ini generation settings
16+
# @return [String] Converted data as ini string
17+
dispatch :to_ini do
18+
required_param 'Hash', :data
19+
optional_param 'Hash', :settings
20+
return_type 'String'
21+
end
22+
23+
def to_ini(data, settings = {})
24+
default_settings = {
25+
'header' => '# THIS FILE IS CONTROLLED BY PUPPET',
26+
'section_prefix' => '[',
27+
'section_suffix' => ']',
28+
'key_val_separator' => '=',
29+
'quote_char' => '"',
30+
'quote_booleans' => true,
31+
'quote_numerics' => true,
32+
}
33+
34+
settings = default_settings.merge(settings)
35+
36+
ini = []
37+
ini << settings['header'] << nil
38+
data.each_key do |section|
39+
ini << "#{settings['section_prefix']}#{section}#{settings['section_suffix']}"
40+
data[section].each do |k, v|
41+
v_is_a_boolean = (v.is_a?(TrueClass) or v.is_a?(FalseClass))
42+
43+
ini << if (v_is_a_boolean && !(settings['quote_booleans'])) || (v.is_a?(Numeric) && !(settings['quote_numerics']))
44+
"#{k}#{settings['key_val_separator']}#{v}"
45+
else
46+
"#{k}#{settings['key_val_separator']}#{settings['quote_char']}#{v}#{settings['quote_char']}"
47+
end
48+
end
49+
ini << nil
50+
end
51+
ini.join("\n")
52+
end
53+
end

spec/functions/extlib/to_ini_spec.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'extlib::to_ini' do
6+
let(:example_input) do
7+
{
8+
'main' => {
9+
'logging' => 'INFO',
10+
'limit' => 314,
11+
'awesome' => true,
12+
},
13+
'dev' => {
14+
'logging' => 'DEBUG',
15+
'log_location' => '/var/log/dev.log',
16+
}
17+
}
18+
end
19+
20+
it { is_expected.not_to eq(nil) }
21+
22+
context 'default settings' do
23+
let(:output) do
24+
<<~EOS
25+
# THIS FILE IS CONTROLLED BY PUPPET
26+
27+
[main]
28+
logging="INFO"
29+
limit="314"
30+
awesome="true"
31+
32+
[dev]
33+
logging="DEBUG"
34+
log_location="/var/log/dev.log"
35+
EOS
36+
end
37+
38+
it { is_expected.to run.with_params(example_input).and_return(output) }
39+
end
40+
41+
context 'custom settings' do
42+
let(:settings) do
43+
{
44+
'header' => '; THIS FILE IS CONTROLLED BY /dev/random',
45+
'section_prefix' => '[[',
46+
'section_suffix' => ']]',
47+
'key_val_separator' => ': ',
48+
'quote_char' => '',
49+
}
50+
end
51+
let(:output) do
52+
<<~EOS
53+
; THIS FILE IS CONTROLLED BY /dev/random
54+
55+
[[main]]
56+
logging: INFO
57+
limit: 314
58+
awesome: true
59+
60+
[[dev]]
61+
logging: DEBUG
62+
log_location: /var/log/dev.log
63+
EOS
64+
end
65+
66+
it { is_expected.to run.with_params(example_input, settings).and_return(output) }
67+
end
68+
69+
context 'default settings with custom quoting' do
70+
let(:settings) do
71+
{
72+
'quote_booleans' => false,
73+
'quote_numerics' => false,
74+
}
75+
end
76+
let(:output) do
77+
<<~EOS
78+
# THIS FILE IS CONTROLLED BY PUPPET
79+
80+
[main]
81+
logging="INFO"
82+
limit=314
83+
awesome=true
84+
85+
[dev]
86+
logging="DEBUG"
87+
log_location="/var/log/dev.log"
88+
EOS
89+
end
90+
91+
it { is_expected.to run.with_params(example_input, settings).and_return(output) }
92+
end
93+
end

0 commit comments

Comments
 (0)