summaryrefslogtreecommitdiff
path: root/lib/puppet/provider/file_line
diff options
context:
space:
mode:
authorJohnson Earls <johnson.earls@oracle.com>2015-08-06 13:00:11 -0700
committerJohnson Earls <johnson.earls@oracle.com>2015-08-06 13:44:32 -0700
commit9bacf14ca24283a94883523064603babcd7046d3 (patch)
treeb8343ca7af2e569efea2a77b4c53bb92a1a8fe47 /lib/puppet/provider/file_line
parent061d0c29fc54391f3e713e9ed76da3933b19083b (diff)
downloadpuppet-stdlib-9bacf14ca24283a94883523064603babcd7046d3.tar.gz
puppet-stdlib-9bacf14ca24283a94883523064603babcd7046d3.tar.bz2
allow `match` parameter to influence `ensure => absent` behavior.
Split the `destroy` method of the file_type::ruby provider into two private methods: `handle_destroy_line` which is the same as the previous `destroy` method, and `handle_destroy_with_match` which will destroy any line which matches the `match` parameter, raising an error if multiple lines match and the `multiple` parameter is not `true`. This new behavior is only used if the new boolean parameter `match_for_absence` is `true` (it defaults to `false`).
Diffstat (limited to 'lib/puppet/provider/file_line')
-rw-r--r--lib/puppet/provider/file_line/ruby.rb26
1 files changed, 23 insertions, 3 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index d4cdfec..aab6fe2 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -22,9 +22,10 @@ Puppet::Type.type(:file_line).provide(:ruby) do
end
def destroy
- local_lines = lines
- File.open(resource[:path],'w') do |fh|
- fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join(''))
+ if resource[:match_for_absence].to_s == 'true' and resource[:match]
+ handle_destroy_with_match
+ else
+ handle_destroy_line
end
end
@@ -93,6 +94,25 @@ Puppet::Type.type(:file_line).provide(:ruby) do
lines.select{|l| l.match(regex)}.size
end
+ def handle_destroy_with_match
+ match_count = count_matches(match_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
+
+ local_lines = lines
+ File.open(resource[:path],'w') do |fh|
+ fh.write(local_lines.reject{|l| match_regex.match(l) }.join(''))
+ end
+ end
+
+ def handle_destroy_line
+ local_lines = lines
+ File.open(resource[:path],'w') do |fh|
+ fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join(''))
+ end
+ end
+
##
# append the line to the file.
#