Merge pull request #18 from cprice-puppet/feature/master/tweaks-to-setting-removal
Feature/master/tweaks to setting removal
This commit is contained in:
commit
a5eebcfca0
4 changed files with 140 additions and 1 deletions
|
@ -12,6 +12,12 @@ Puppet::Type.type(:ini_setting).provide(:ruby) do
|
|||
@ini_file = nil
|
||||
end
|
||||
|
||||
def destroy
|
||||
ini_file.remove_setting(section, setting)
|
||||
ini_file.save
|
||||
@ini_file = nil
|
||||
end
|
||||
|
||||
def value
|
||||
ini_file.get_value(section, setting)
|
||||
end
|
||||
|
|
|
@ -42,10 +42,30 @@ module Util
|
|||
end
|
||||
end
|
||||
|
||||
def remove_setting(section_name, setting)
|
||||
section = @sections_hash[section_name]
|
||||
if (section.has_existing_setting?(setting))
|
||||
# If the setting is found, we have some work to do.
|
||||
# First, we remove the line from our array of lines:
|
||||
remove_line(section, setting)
|
||||
|
||||
# Then, we need to tell the setting object to remove
|
||||
# the setting from its state:
|
||||
section.remove_existing_setting(setting)
|
||||
|
||||
# Finally, we need to update all of the start/end line
|
||||
# numbers for all of the sections *after* the one that
|
||||
# was modified.
|
||||
section_index = @section_names.index(section_name)
|
||||
decrement_section_line_numbers(section_index + 1)
|
||||
end
|
||||
end
|
||||
|
||||
def save
|
||||
File.open(@path, 'w') do |fh|
|
||||
|
||||
@section_names.each do |name|
|
||||
|
||||
section = @sections_hash[name]
|
||||
|
||||
if section.start_line.nil?
|
||||
|
@ -113,6 +133,16 @@ module Util
|
|||
end
|
||||
end
|
||||
|
||||
def remove_line(section, setting)
|
||||
(section.start_line..section.end_line).each do |line_num|
|
||||
if (match = SETTING_REGEX.match(lines[line_num]))
|
||||
if (match[1] == setting)
|
||||
lines.delete_at(line_num)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_line_iter
|
||||
ExternalIterator.new(lines)
|
||||
end
|
||||
|
@ -132,6 +162,17 @@ module Util
|
|||
File.readlines(path)
|
||||
end
|
||||
|
||||
|
||||
# Utility method; given a section index (index into the @section_names
|
||||
# array), decrement the start/end line numbers for that section and all
|
||||
# all of the other sections that appear *after* the specified section.
|
||||
def decrement_section_line_numbers(section_index)
|
||||
@section_names[section_index..(@section_names.length - 1)].each do |name|
|
||||
section = @sections_hash[name]
|
||||
section.decrement_line_nums
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,11 +24,31 @@ class IniFile
|
|||
@existing_settings[setting_name] = value
|
||||
end
|
||||
|
||||
def remove_existing_setting(setting_name)
|
||||
if (@existing_settings.delete(setting_name))
|
||||
if @end_line
|
||||
@end_line = @end_line - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def set_additional_setting(setting_name, value)
|
||||
@additional_settings[setting_name] = value
|
||||
end
|
||||
|
||||
# Decrement the start and end line numbers for the section (if they are
|
||||
# defined); this is intended to be called when a setting is removed
|
||||
# from a section that comes before this section in the ini file.
|
||||
def decrement_line_nums()
|
||||
if @start_line
|
||||
@start_line = @start_line - 1
|
||||
end
|
||||
if @end_line
|
||||
@end_line = @end_line - 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -458,7 +458,79 @@ bar=baz
|
|||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "when ensuring that a setting is absent" do
|
||||
let(:orig_content) {
|
||||
<<-EOS
|
||||
[section1]
|
||||
; This is also a comment
|
||||
foo=foovalue
|
||||
|
||||
bar = barvalue
|
||||
master = true
|
||||
[section2]
|
||||
|
||||
foo= foovalue2
|
||||
baz=bazvalue
|
||||
url = http://192.168.1.1:8080
|
||||
[section:sub]
|
||||
subby=bar
|
||||
#another comment
|
||||
; yet another comment
|
||||
EOS
|
||||
}
|
||||
|
||||
it "should remove a setting that exists" do
|
||||
resource = Puppet::Type::Ini_setting.new(common_params.merge(
|
||||
:section => 'section1', :setting => 'foo', :ensure => 'absent'))
|
||||
provider = described_class.new(resource)
|
||||
provider.exists?.should be_true
|
||||
provider.destroy
|
||||
validate_file(<<-EOS
|
||||
[section1]
|
||||
; This is also a comment
|
||||
|
||||
bar = barvalue
|
||||
master = true
|
||||
[section2]
|
||||
|
||||
foo= foovalue2
|
||||
baz=bazvalue
|
||||
url = http://192.168.1.1:8080
|
||||
[section:sub]
|
||||
subby=bar
|
||||
#another comment
|
||||
; yet another comment
|
||||
EOS
|
||||
)
|
||||
end
|
||||
|
||||
it "should do nothing for a setting that does not exist" do
|
||||
resource = Puppet::Type::Ini_setting.new(common_params.merge(
|
||||
:section => 'section:sub', :setting => 'foo', :ensure => 'absent'))
|
||||
provider = described_class.new(resource)
|
||||
provider.exists?.should be_nil
|
||||
provider.destroy
|
||||
validate_file(<<-EOS
|
||||
[section1]
|
||||
; This is also a comment
|
||||
foo=foovalue
|
||||
|
||||
bar = barvalue
|
||||
master = true
|
||||
[section2]
|
||||
|
||||
foo= foovalue2
|
||||
baz=bazvalue
|
||||
url = http://192.168.1.1:8080
|
||||
[section:sub]
|
||||
subby=bar
|
||||
#another comment
|
||||
; yet another comment
|
||||
EOS
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue