]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
MODULES-1248 Fix issue with not properly counting regex matches with legacy versions...
authorTravis Fields <travis@puppetlabs.com>
Tue, 16 Sep 2014 17:46:19 +0000 (10:46 -0700)
committerTravis Fields <travis@puppetlabs.com>
Tue, 16 Sep 2014 20:41:39 +0000 (13:41 -0700)
lib/puppet/provider/file_line/ruby.rb

index 94e7fac9196ec454cd9928951c15176886a7c633..ae1a8b3dbf3a56afec107341d1c331796cbc6d07 100644 (file)
@@ -34,7 +34,7 @@ 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
+    match_count = count_matches(regex)
     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
@@ -51,9 +51,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do
 
   def handle_create_with_after
     regex = Regexp.new(resource[:after])
-
-    count = lines.count {|l| l.match(regex)}
-
+    count = count_matches(regex)
     case count
     when 1 # find the line to put our line after
       File.open(resource[:path], 'w') do |fh|
@@ -71,6 +69,10 @@ Puppet::Type.type(:file_line).provide(:ruby) do
     end
   end
 
+  def count_matches(regex)
+    lines.select{|l| l.match(regex)}.size
+  end
+
   ##
   # append the line to the file.
   #