summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Bode <dan@puppetlabs.com>2012-10-10 17:40:38 -0700
committerDan Bode <dan@puppetlabs.com>2012-10-10 17:40:38 -0700
commit1106d70e881028ee2dfa476307444780c9c4cbaa (patch)
treed58ff7e2e0e2a96f8bed7c26c09c5962626d5dd0
parentcbc90d38347f8ca61de2208b16ffc76f9fd5938c (diff)
downloadpuppet-inifile-1106d70e881028ee2dfa476307444780c9c4cbaa.tar.gz
puppet-inifile-1106d70e881028ee2dfa476307444780c9c4cbaa.tar.bz2
Add support for removing lines
This commit adds the ability to ensure that lines are absent.
-rw-r--r--lib/puppet/provider/ini_setting/ruby.rb6
-rw-r--r--lib/puppet/util/ini_file.rb23
-rw-r--r--lib/puppet/util/ini_file/section.rb6
-rw-r--r--spec/unit/puppet/provider/ini_setting/ruby_spec.rb46
4 files changed, 79 insertions, 2 deletions
diff --git a/lib/puppet/provider/ini_setting/ruby.rb b/lib/puppet/provider/ini_setting/ruby.rb
index 946685e..2c13847 100644
--- a/lib/puppet/provider/ini_setting/ruby.rb
+++ b/lib/puppet/provider/ini_setting/ruby.rb
@@ -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
diff --git a/lib/puppet/util/ini_file.rb b/lib/puppet/util/ini_file.rb
index b2e554f..107924d 100644
--- a/lib/puppet/util/ini_file.rb
+++ b/lib/puppet/util/ini_file.rb
@@ -42,17 +42,28 @@ module Util
end
end
+ def remove_setting(section_name, setting)
+ section = @sections_hash[section_name]
+ if (section.has_existing_setting?(setting))
+ remove_line(section, setting)
+ section.remove_existing_setting(setting)
+ end
+ end
+
def save
File.open(@path, 'w') do |fh|
@section_names.each do |name|
+
section = @sections_hash[name]
if section.start_line.nil?
fh.puts("\n[#{section.name}]")
elsif ! section.end_line.nil?
(section.start_line..section.end_line).each do |line_num|
- fh.puts(lines[line_num])
+ if lines[line_num]
+ fh.puts(lines[line_num])
+ end
end
end
@@ -113,6 +124,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
diff --git a/lib/puppet/util/ini_file/section.rb b/lib/puppet/util/ini_file/section.rb
index 39f2959..e192d5b 100644
--- a/lib/puppet/util/ini_file/section.rb
+++ b/lib/puppet/util/ini_file/section.rb
@@ -24,6 +24,10 @@ class IniFile
@existing_settings[setting_name] = value
end
+ def remove_existing_setting(setting_name)
+ @existing_settings.delete(setting_name)
+ end
+
def set_additional_setting(setting_name, value)
@additional_settings[setting_name] = value
end
@@ -31,4 +35,4 @@ class IniFile
end
end
end
-end \ No newline at end of file
+end
diff --git a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb
index 4062205..c8fcec4 100644
--- a/spec/unit/puppet/provider/ini_setting/ruby_spec.rb
+++ b/spec/unit/puppet/provider/ini_setting/ruby_spec.rb
@@ -458,7 +458,53 @@ 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
end
end