summaryrefslogtreecommitdiff
path: root/lib/puppet/provider/file_line/ruby.rb
diff options
context:
space:
mode:
authorBryan Jen <bryan.jen@gmail.com>2015-08-13 10:14:13 -0700
committerBryan Jen <bryan.jen@gmail.com>2015-08-13 10:14:13 -0700
commite84090df1d081679e0c687614efb982354cf3cbe (patch)
tree87fab8db19e9dcec2bcf64bedfb35c5fc1ccc437 /lib/puppet/provider/file_line/ruby.rb
parent605fffd852e972a2df76be1b051d3b4a5f740bd5 (diff)
parent9bacf14ca24283a94883523064603babcd7046d3 (diff)
downloadpuppet-stdlib-e84090df1d081679e0c687614efb982354cf3cbe.tar.gz
puppet-stdlib-e84090df1d081679e0c687614efb982354cf3cbe.tar.bz2
Merge pull request #499 from jearls/2370-use-match-for-ensure-absent
[MODULES-2370] allow `match` parameter to influence `ensure => absent` behavior.
Diffstat (limited to 'lib/puppet/provider/file_line/ruby.rb')
-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.
#