]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Update file_line resource to support 'after'.
authorDan Prince <dprince@redhat.com>
Thu, 29 Aug 2013 16:19:16 +0000 (12:19 -0400)
committerJeff McCune <jeff@puppetlabs.com>
Thu, 29 Aug 2013 22:07:29 +0000 (15:07 -0700)
When adding new lines to a file the 'after' option can be useful
when you need to insert file lines into the middle of a file.

This is particularly helpful when using file_line with sectioned
config files.

NOTE: the after option only works when adding new lines. If you are
updating an existing (matched) line it will simply modify it in place.
This assumes it was in the right place to begin with.

lib/puppet/provider/file_line/ruby.rb
lib/puppet/type/file_line.rb
spec/unit/puppet/provider/file_line/ruby_spec.rb

index 3cb9f6ee6e343810bc634efe29557eb89ed25d28..7ddb39f3d69b791a119bfd086ea85c829af263cb 100644 (file)
@@ -49,10 +49,32 @@ Puppet::Type.type(:file_line).provide(:ruby) do
   end
 
   def handle_create_without_match
-    File.open(resource[:path], 'a') do |fh|
-      fh.puts resource[:line]
+
+    regex = resource[:after] ? Regexp.new(resource[:after]) : nil
+    after_count = File.exists?(resource[:path]) ? lines.select { |l| regex.match(l) }.size : 0
+    if after_count > 1 then
+     raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches after pattern '#{resource[:after]}'"
     end
-  end
 
+    if (after_count == 0)
+
+      File.open(resource[:path], 'a') do |fh|
+        fh.puts resource[:line]
+      end
+
+    else
+
+      File.open(resource[:path], 'w') do |fh|
+        lines.each do |l|
+          fh.puts(l)
+          if regex.match(l) then
+            fh.puts(resource[:line])
+          end
+        end
+      end
+
+    end
+
+  end
 
 end
index 14946bb9d0315cf57de098f3f25a604f61c95f50..323fc4c9c7f7054e2bac68d10333b9c6290b8dae 100644 (file)
@@ -42,6 +42,10 @@ Puppet::Type.newtype(:file_line) do
     newvalues(true, false)
   end
 
+  newparam(:after) do
+    desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)'
+  end
+
   newparam(:line) do
     desc 'The line to be appended to the file located by the path parameter.'
   end
index 648c05b4369a0da5f5f857e22d0980f2f1c26ec1..c8575aba86779922a00bce33c5b8ca713784251d 100644 (file)
@@ -80,6 +80,24 @@ describe provider_class do
       File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
     end
 
+    it 'should replace all lines that matches with after' do
+      @resource = Puppet::Type::File_line.new(
+          {
+           :name => 'foo',
+           :path => @tmpfile,
+           :line => 'inserted = line',
+           :after => '^foo1',
+          }
+      )
+      @provider = provider_class.new(@resource)
+      File.open(@tmpfile, 'w') do |fh|
+        fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
+      end
+      @provider.exists?.should be_nil
+      @provider.create
+      File.read(@tmpfile).chomp.should eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz")
+    end
+
     it 'should raise an error with invalid values' do
       expect {
         @resource = Puppet::Type::File_line.new(