Skip to content

Commit

Permalink
Update inifile to 033722a
Browse files Browse the repository at this point in the history
033722a Merge pull request redhat-openstack#159 from WhatsARanjit/MODULES-1940
b0fb47d 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.

Change-Id: I0ff3cb3225851a252f8a38499b38be07db5187e7
  • Loading branch information
xbezdick committed May 4, 2015
1 parent ae4a04e commit de6d037
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Puppetfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mod 'horizon',
:git => 'https://github.com/stackforge/puppet-horizon.git'

mod 'inifile',
:commit => '5d70bafff50a3cf66f114550ff0268d8b5eeba19',
:commit => '033722a34a4a3bae6cc5bcb36995482d8c2b15af',
:git => 'https://github.com/puppetlabs/puppetlabs-inifile.git'

mod 'ipa',
Expand Down
28 changes: 28 additions & 0 deletions inifile/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 inifile/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 inifile/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 inifile/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 de6d037

Please sign in to comment.