]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
(#21416) Allow file_line to match multiple lines
authorAlex O'Rielly <aorielly@gmail.com>
Fri, 21 Jun 2013 22:33:44 +0000 (17:33 -0500)
committerAdrien Thebo <git@somethingsinistral.net>
Thu, 11 Jul 2013 23:25:56 +0000 (16:25 -0700)
Without this commit the file_line type will outright fail if multiple
lines match the given regex. This commit allows the file_line type and
provider to optionally match and modify all matching lines.

Changeset rebased into a single commit by Adrien Thebo <adrien@puppetlabs.com>

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

index a3219d3d4d44133bd9608fd05004c18556c12a21..3cb9f6ee6e343810bc634efe29557eb89ed25d28 100644 (file)
@@ -1,4 +1,3 @@
-
 Puppet::Type.type(:file_line).provide(:ruby) do
 
   def exists?
@@ -35,8 +34,8 @@ Puppet::Type.type(:file_line).provide(:ruby) do
   def handle_create_with_match()
     regex = resource[:match] ? Regexp.new(resource[:match]) : nil
     match_count = lines.select { |l| regex.match(l) }.size
-    if match_count > 1
-      raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
+    if match_count > 1 && resource[:multiple].to_s != 'true'
+     raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
     end
     File.open(resource[:path], 'w') do |fh|
       lines.each do |l|
index f71a4bc7b6f2c264a88d355f023a04d7e8b7004d..14946bb9d0315cf57de098f3f25a604f61c95f50 100644 (file)
@@ -37,6 +37,11 @@ Puppet::Type.newtype(:file_line) do
         'if a match is found, we replace that line rather than adding a new line.'
   end
 
+  newparam(:multiple) do
+    desc 'An optional value to determine if match can change multiple lines.'
+    newvalues(true, false)
+  end
+
   newparam(:line) do
     desc 'The line to be appended to the file located by the path parameter.'
   end
index 7857d399fc347b4e60873f6feba106099eefebac..648c05b4369a0da5f5f857e22d0980f2f1c26ec1 100644 (file)
@@ -61,6 +61,39 @@ describe provider_class do
       File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
     end
 
+    it 'should replace all lines that matches' do
+      @resource = Puppet::Type::File_line.new(
+          {
+           :name => 'foo',
+           :path => @tmpfile,
+           :line => 'foo = bar',
+           :match => '^foo\s*=.*$',
+           :multiple => true
+          }
+      )
+      @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\nfoo = bar\nfoo2\nfoo = bar")
+    end
+
+    it 'should raise an error with invalid values' do
+      expect {
+        @resource = Puppet::Type::File_line.new(
+          {
+           :name => 'foo',
+           :path => @tmpfile,
+           :line => 'foo = bar',
+           :match => '^foo\s*=.*$',
+           :multiple => 'asgadga'
+          }
+        )
+      }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
+    end
+
     it 'should replace a line that matches' do
       File.open(@tmpfile, 'w') do |fh|
         fh.write("foo1\nfoo=blah\nfoo2")