Skip to content

Commit

Permalink
Merge pull request #321 from cyberious/4.3.x
Browse files Browse the repository at this point in the history
MODULES-1248 Fix issue with not properly counting regex matches with leg...
  • Loading branch information
hunner committed Sep 16, 2014
2 parents 4a79fd0 + acf435d commit c1ff630
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/puppet/provider/file_line/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def lines

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
Expand All @@ -51,9 +51,7 @@ def handle_create_with_match()

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|
Expand All @@ -71,6 +69,10 @@ def handle_create_with_after
end
end

def count_matches(regex)
lines.select{|l| l.match(regex)}.size
end

##
# append the line to the file.
#
Expand Down

0 comments on commit c1ff630

Please sign in to comment.