Skip to content

Commit

Permalink
Merge pull request redhat-openstack#159 from WhatsARanjit/MODULES-1940
Browse files Browse the repository at this point in the history
Adding the ability to change regex match for $section in inifile
  • Loading branch information
tphoney committed Apr 21, 2015
2 parents 5d70baf + b0fb47d commit 033722a
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 5 deletions.
28 changes: 28 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ ini_subsetting {'sample subsetting':
}
~~~

###Use a non-standard section header

~~~
default:
minage = 1
maxage = 13
ini_setting { 'default minage':
ensure => present,
path => '/etc/security/users',
section => 'default',
setting => 'minage',
value => '1',
section_prefix => '',
section_suffix => ':',
}
~~~

###Implement child providers


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

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

##### `section_prefix`

*Optional.* Designates the string that will appear before the section's name. Default value: "["

##### `section_suffix`

*Optional.* Designates the string that will appear after the section's name. Default value: "]"

**NOTE:** The way this type finds all sections in the file is by looking for lines like `${section_prefix}${title}${section_suffix}`

### Type: ini_subsetting


Expand Down
18 changes: 17 additions & 1 deletion lib/puppet/provider/ini_setting/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,25 @@ def separator
end
end

def section_prefix
if resource.class.validattr?(:section_prefix)
resource[:section_prefix] || '['
else
'['
end
end

def section_suffix
if resource.class.validattr?(:section_suffix)
resource[:section_suffix] || ']'
else
']'
end
end

private
def ini_file
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator)
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator, section_prefix, section_suffix)
end

end
11 changes: 11 additions & 0 deletions lib/puppet/type/ini_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,16 @@
desc 'The value of the setting to be defined.'
end

newparam(:section_prefix) do
desc 'The prefix to the section name\'s header.' +
'Defaults to \'[\'.'
defaultto('[')
end

newparam(:section_suffix) do
desc 'The suffix to the section name\'s header.' +
'Defaults to \']\'.'
defaultto(']')
end

end
29 changes: 26 additions & 3 deletions lib/puppet/util/ini_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ module Puppet
module Util
class IniFile

def initialize(path, key_val_separator = ' = ')
def initialize(path, key_val_separator = ' = ', section_prefix = '[', section_suffix = ']')

k_v_s = key_val_separator.strip

@@SECTION_REGEX = /^\s*\[([^\]]*)\]\s*$/
@section_prefix = section_prefix
@section_suffix = section_suffix

@@SECTION_REGEX = section_regex
@@SETTING_REGEX = /^(\s*)([^#;\s]|[^#;\s].*?[^\s#{k_v_s}])(\s*#{k_v_s}\s*)(.*)\s*$/
@@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)(.*?[^\s#{k_v_s}])(\s*#{k_v_s}[ \t]*)(.*)\s*$/

Expand All @@ -22,6 +25,26 @@ def initialize(path, key_val_separator = ' = ')
end
end

def section_regex
# Only put in prefix/suffix if they exist
# Also, if the prefix is '', the negated
# set match should be a match all instead.
r_string = '^\s*'
r_string += Regexp.escape(@section_prefix)
r_string += '('
if @section_prefix != ''
r_string += '[^'
r_string += Regexp.escape(@section_prefix)
r_string += ']'
else
r_string += '.'
end
r_string += '*)'
r_string += Regexp.escape(@section_suffix)
r_string += '\s*$'
/#{r_string}/
end

def section_names
@section_names
end
Expand Down Expand Up @@ -107,7 +130,7 @@ def save
whitespace_buffer = []

if (section.is_new_section?) && (! section.is_global?)
fh.puts("\n[#{section.name}]")
fh.puts("\n#{@section_prefix}#{section.name}#{@section_suffix}")
end

if ! section.is_new_section?
Expand Down
Loading

0 comments on commit 033722a

Please sign in to comment.