diff options
author | David Schmitt <david.schmitt@puppetlabs.com> | 2015-05-29 10:21:41 +0100 |
---|---|---|
committer | David Schmitt <david.schmitt@puppetlabs.com> | 2015-05-29 10:21:41 +0100 |
commit | 4a8c0a57f2c4df4e2e3fa37466f02a7f6fccd517 (patch) | |
tree | ee3a9e21d41ee70b94e8d8eb2dcd7a94e2bdeb82 /lib | |
parent | c9b810cf365cbc4f87dfcee8b4eedf0b055e3569 (diff) | |
parent | 72089f3d134a00e64f0b3d81237a266131d40412 (diff) | |
download | puppet-stdlib-4a8c0a57f2c4df4e2e3fa37466f02a7f6fccd517.tar.gz puppet-stdlib-4a8c0a57f2c4df4e2e3fa37466f02a7f6fccd517.tar.bz2 |
Merge pull request #463 from CENGN/fix/master/file_line_multiple_after
(MODULES-2071) Patch file_line provider to use multiple with after
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/provider/file_line/ruby.rb | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index e7854f0..c58e27e 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -61,20 +61,22 @@ Puppet::Type.type(:file_line).provide(:ruby) do def handle_create_with_after regex = Regexp.new(resource[:after]) count = count_matches(regex) - case count - when 1 # find the line to put our line after - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) - if regex.match(l) then - fh.puts(resource[:line]) - end + + if count > 1 && resource[:multiple].to_s != 'true' + raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." + end + + File.open(resource[:path], 'w') do |fh| + lines.each do |l| + fh.puts(l) + if regex.match(l) then + fh.puts(resource[:line]) end end - when 0 # append the line to the end of the file + end + + if (count == 0) # append the line to the end of the file append_line - else - raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." end end |