diff options
author | Morgan Haskel <morgan@puppetlabs.com> | 2015-04-09 16:47:34 -0700 |
---|---|---|
committer | Morgan Haskel <morgan@puppetlabs.com> | 2015-04-09 16:47:34 -0700 |
commit | 5ee6e960f69fbee3ca53496c4437f98784aeb738 (patch) | |
tree | ec3a00dbe32be65a3a2bf74b493746a35a67ac58 /lib/puppet | |
parent | acf57bbe31f9b51792965b47cda6774d3bd59999 (diff) | |
parent | 0af0d7e5392a69f0ed72fa1b0225fe2a61188319 (diff) |
Merge pull request #431 from bmjen/file-line-refactor
File_line checks provided after param if no match is found
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/provider/file_line/ruby.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index ae1a8b3..e7854f0 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -34,13 +34,22 @@ Puppet::Type.type(:file_line).provide(:ruby) do 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) @@ -78,7 +87,10 @@ Puppet::Type.type(:file_line).provide(:ruby) do # # @api private def append_line - File.open(resource[:path], 'a') do |fh| + File.open(resource[:path], 'w') do |fh| + lines.each do |l| + fh.puts(l) + end fh.puts resource[:line] end end |