Skip to content

Commit 9c6d652

Browse files
committed
Added ability to specify section_prefix and section_suffix to accomodate files with sections that don't follow [section_name] format. AIX for example uses section_name: format.
1 parent f1e4ff4 commit 9c6d652

5 files changed

Lines changed: 375 additions & 5 deletions

File tree

README.markdown

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ ini_subsetting {'sample subsetting':
6565
}
6666
~~~
6767

68+
###Use a non-standard section header
69+
70+
~~~
71+
default:
72+
minage = 1
73+
maxage = 13
74+
75+
ini_setting { 'default minage':
76+
ensure => present,
77+
path => '/etc/security/users',
78+
section => 'default',
79+
setting => 'minage',
80+
value => '1',
81+
section_prefix => '',
82+
section_suffix => ':',
83+
}
84+
~~~
85+
6886
###Implement child providers
6987

7088

@@ -176,6 +194,16 @@ Determines whether the specified setting should exist. Valid options: 'present'
176194

177195
*Optional.* Supplies a value for the specified setting. Valid options: a string. Default value: undefined.
178196

197+
##### `section_prefix`
198+
199+
*Optional.* Designates the string that will appear before the section's name. Default value: "["
200+
201+
##### `section_suffix`
202+
203+
*Optional.* Designates the string that will appear after the section's name. Default value: "]"
204+
205+
**NOTE:** The way this type finds all sections in the file is by looking for lines like `${section_prefix}${title}${section_suffix}`
206+
179207
### Type: ini_subsetting
180208

181209

lib/puppet/provider/ini_setting/ruby.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,25 @@ def separator
9494
end
9595
end
9696

97+
def section_prefix
98+
if resource.class.validattr?(:section_prefix)
99+
resource[:section_prefix] || '['
100+
else
101+
'['
102+
end
103+
end
104+
105+
def section_suffix
106+
if resource.class.validattr?(:section_suffix)
107+
resource[:section_suffix] || ']'
108+
else
109+
']'
110+
end
111+
end
112+
97113
private
98114
def ini_file
99-
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator)
115+
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator, section_prefix, section_suffix)
100116
end
101117

102118
end

lib/puppet/type/ini_setting.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,16 @@
3939
desc 'The value of the setting to be defined.'
4040
end
4141

42+
newparam(:section_prefix) do
43+
desc 'The prefix to the section name\'s header.' +
44+
'Defaults to \'[\'.'
45+
defaultto('[')
46+
end
47+
48+
newparam(:section_suffix) do
49+
desc 'The suffix to the section name\'s header.' +
50+
'Defaults to \']\'.'
51+
defaultto(']')
52+
end
4253

4354
end

lib/puppet/util/ini_file.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ module Puppet
55
module Util
66
class IniFile
77

8-
def initialize(path, key_val_separator = ' = ')
8+
def initialize(path, key_val_separator = ' = ', section_prefix = '[', section_suffix = ']')
99

1010
k_v_s = key_val_separator.strip
1111

12-
@@SECTION_REGEX = /^\s*\[([^\]]*)\]\s*$/
12+
@section_prefix = section_prefix
13+
@section_suffix = section_suffix
14+
15+
@@SECTION_REGEX = section_regex
1316
@@SETTING_REGEX = /^(\s*)([^#;\s]|[^#;\s].*?[^\s#{k_v_s}])(\s*#{k_v_s}\s*)(.*)\s*$/
1417
@@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)(.*?[^\s#{k_v_s}])(\s*#{k_v_s}[ \t]*)(.*)\s*$/
1518

@@ -22,6 +25,26 @@ def initialize(path, key_val_separator = ' = ')
2225
end
2326
end
2427

28+
def section_regex
29+
# Only put in prefix/suffix if they exist
30+
# Also, if the prefix is '', the negated
31+
# set match should be a match all instead.
32+
r_string = '^\s*'
33+
r_string += Regexp.escape(@section_prefix)
34+
r_string += '('
35+
if @section_prefix != ''
36+
r_string += '[^'
37+
r_string += Regexp.escape(@section_prefix)
38+
r_string += ']'
39+
else
40+
r_string += '.'
41+
end
42+
r_string += '*)'
43+
r_string += Regexp.escape(@section_suffix)
44+
r_string += '\s*$'
45+
/#{r_string}/
46+
end
47+
2548
def section_names
2649
@section_names
2750
end
@@ -107,7 +130,7 @@ def save
107130
whitespace_buffer = []
108131

109132
if (section.is_new_section?) && (! section.is_global?)
110-
fh.puts("\n[#{section.name}]")
133+
fh.puts("\n#{@section_prefix}#{section.name}#{@section_suffix}")
111134
end
112135

113136
if ! section.is_new_section?

0 commit comments

Comments
 (0)