summaryrefslogtreecommitdiff
path: root/lib/puppet/util/ini_file.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/util/ini_file.rb')
-rw-r--r--lib/puppet/util/ini_file.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb
index b2e554f..52ad32c 100644
--- a/lib/puppet/util/ini_file.rb
+++ b/lib/puppet/util/ini_file.rb
@@ -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