]> gitweb.fluxo.info Git - puppet-inifile.git/commitdiff
Add support for removing lines
authorDan Bode <dan@puppetlabs.com>
Thu, 11 Oct 2012 00:40:38 +0000 (17:40 -0700)
committerDan Bode <dan@puppetlabs.com>
Thu, 11 Oct 2012 00:40:38 +0000 (17:40 -0700)
This commit adds the ability to ensure that lines are absent.

lib/puppet/provider/ini_setting/ruby.rb
lib/puppet/util/ini_file.rb
lib/puppet/util/ini_file/section.rb
spec/unit/puppet/provider/ini_setting/ruby_spec.rb

index 946685e12e44ae1c0fabde49fe81c87f39fad760..2c1384782bec9fb007cddf027f66b79eed359aae 100644 (file)
@@ -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
index b2e554f239e8595858df03ded81355714c0084b4..107924d34bfc7adf80a16de78d1156719f128597 100644 (file)
@@ -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
index 39f2959b91a7353124957418bc17e078c6acda94..e192d5bd4e142ae150ffe200e082ce118ff0773d 100644 (file)
@@ -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
index 40622059ab058886395c302bba9e29b19ded444c..c8fcec41030c7df3041a15cb27936ae41e79baa4 100644 (file)
@@ -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