private
def ini_file
- @ini_file ||= Puppet::Util::IniFile.new(resource[:path])
+ @ini_file ||= Puppet::Util::IniFile.new(resource[:path],
+ resource[:key_val_separator])
end
end
end
end
-end
\ No newline at end of file
+ newparam(:key_val_separator) do
+ desc 'The separator string to use between each setting name and value. ' +
+ 'Defaults to " = ", but you could use this to override e.g. whether ' +
+ 'or not the separator should include whitespace.'
+ defaultto(" = ")
+ end
+
+end
SECTION_REGEX = /^\s*\[([\w\d\.\\\/\-\:]+)\]\s*$/
SETTING_REGEX = /^\s*([\w\d\.\\\/\-]+)\s*=\s*([\S]+)\s*$/
- def initialize(path)
+ def initialize(path, key_val_separator = ' = ')
@path = path
+ @key_val_separator = key_val_separator
@section_names = []
@sections_hash = {}
if File.file?(@path)
end
section.additional_settings.each_pair do |key, value|
- fh.puts("#{key} = #{value}")
+ fh.puts("#{key}#{@key_val_separator}#{value}")
end
end
end
(section.start_line..section.end_line).each do |line_num|
if (match = SETTING_REGEX.match(lines[line_num]))
if (match[1] == setting)
- lines[line_num] = "#{setting} = #{value}"
+ lines[line_num] = "#{setting}#{@key_val_separator}#{value}"
end
end
end
end
end
+ context "when overriding the separator" do
+ let(:orig_content) {
+ <<-EOS
+[section2]
+foo=bar
+ EOS
+ }
+
+ it "should modify an existing setting" do
+ resource = Puppet::Type::Ini_setting.new(common_params.merge(
+ :section => 'section2',
+ :setting => 'foo',
+ :value => 'yippee',
+ :key_val_separator => '='))
+ provider = described_class.new(resource)
+ provider.exists?.should == false
+ provider.create
+ validate_file(<<-EOS
+[section2]
+foo=yippee
+ EOS
+ )
+ end
+
+ it "should add a new setting" do
+ resource = Puppet::Type::Ini_setting.new(common_params.merge(
+ :section => 'section2',
+ :setting => 'bar',
+ :value => 'baz',
+ :key_val_separator => '='))
+ provider = described_class.new(resource)
+ provider.exists?.should == false
+ provider.create
+ validate_file(<<-EOS
+[section2]
+foo=bar
+bar=baz
+ EOS
+ )
+ end
+
+
+ end
+
end
}
ini_setting { "sample setting2":
- path => '/tmp/foo.ini',
- section => 'bar',
- setting => 'barsetting',
- value => 'BAR!',
- ensure => present,
- require => Ini_setting["sample setting"],
+ path => '/tmp/foo.ini',
+ section => 'bar',
+ setting => 'barsetting',
+ value => 'BAR!',
+ key_value_separator => '=',
+ ensure => present,
+ require => Ini_setting["sample setting"],
}