Skip to content

Commit

Permalink
file_line honors after if match not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmjen committed Apr 9, 2015
1 parent 8fba5c0 commit 35303ce
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/puppet/provider/file_line/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ def lines

def handle_create_with_match()
regex = resource[:match] ? Regexp.new(resource[:match]) : nil
regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil
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

File.open(resource[:path], 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? resource[:line] : l)
if (match_count == 0 and regex_after)
if regex_after.match(l)
fh.puts(resource[:line])
match_count += 1 #Increment match_count to indicate that the new line has been inserted.
end
end
end

if (match_count == 0)
Expand Down

0 comments on commit 35303ce

Please sign in to comment.