From 35303ce0f7cf66feb7499d9671a7d2121a0f52b1 Mon Sep 17 00:00:00 2001 From: Bryan Jen Date: Thu, 9 Apr 2015 11:30:22 -0700 Subject: file_line honors after if match not found. --- lib/puppet/provider/file_line/ruby.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/puppet/provider/file_line/ruby.rb') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index ae1a8b3..a1acab8 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) -- cgit v1.2.3 From 0af0d7e5392a69f0ed72fa1b0225fe2a61188319 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Thu, 9 Apr 2015 11:02:29 -0700 Subject: Add spec tests and pulled in PR #427 Changed append line to open in 'w' mode and have to rewrite lines in order to append new line --- lib/puppet/provider/file_line/ruby.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/file_line/ruby.rb') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index a1acab8..e7854f0 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -87,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 -- cgit v1.2.3 From 72089f3d134a00e64f0b3d81237a266131d40412 Mon Sep 17 00:00:00 2001 From: Raymond Maika Date: Fri, 29 May 2015 00:27:08 -0400 Subject: (MODULES-2071) Refactor file_line provider to contain logic to handle parameter multiple in function handle_create_with_after Without this, file_line resource without the `match` parameter but with the `after` param will throw an error if there are multiple matches for the after expression. This patch creates the handling for the `multiple` parameter in handle_create_with_after. This allows you to add a line after the `after` expression if it appears at multiple points in a file. Updated reference to `file_line` in the README to reflect that the multiple parameter can be set when using `after` and/or `match` as the matching regex. --- lib/puppet/provider/file_line/ruby.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'lib/puppet/provider/file_line/ruby.rb') 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 -- cgit v1.2.3 From 35e92645f727f02ef9ace8948154079bc0fff05a Mon Sep 17 00:00:00 2001 From: Raymond Maika Date: Thu, 30 Jul 2015 14:05:39 -0400 Subject: (MODULES-2024) Adding replace attribute to file_line --- lib/puppet/provider/file_line/ruby.rb | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'lib/puppet/provider/file_line/ruby.rb') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index c58e27e..ea1d44d 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,17 +1,23 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? - lines.find do |line| - line.chomp == resource[:line].chomp + if !resource[:replace] and count_matches(match_regex) > 0 + true + else + lines.find do |line| + line.chomp == resource[:line].chomp + end end end def create - if resource[:match] - handle_create_with_match - elsif resource[:after] - handle_create_with_after - else - append_line + unless !resource[:replace] and count_matches(match_regex) > 0 + if resource[:match] + handle_create_with_match + elsif resource[:after] + handle_create_with_after + else + append_line + end end end @@ -32,10 +38,13 @@ Puppet::Type.type(:file_line).provide(:ruby) do @lines ||= File.readlines(resource[:path]) end + def match_regex + resource[:match] ? Regexp.new(resource[:match]) : nil + end + 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) + 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]}'" @@ -43,7 +52,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do File.open(resource[:path], 'w') do |fh| lines.each do |l| - fh.puts(regex.match(l) ? resource[:line] : l) + fh.puts(match_regex.match(l) ? resource[:line] : l) if (match_count == 0 and regex_after) if regex_after.match(l) fh.puts(resource[:line]) -- cgit v1.2.3 From a7adcda803abe82e6a16e2410c10d58abedbd82d Mon Sep 17 00:00:00 2001 From: Dominic Cleal Date: Tue, 4 Aug 2015 09:59:53 +0100 Subject: (MODULES-2316) Change file_type boolean parameter to symbols Puppet's boolean parameter type is only available in Puppet 3.3 and higher, so change file_type's new "replace" parameter to a regular parameter with true and false as possible values. This matches the existing "multiple" parameter. --- lib/puppet/provider/file_line/ruby.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/provider/file_line/ruby.rb') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index ea1d44d..d4cdfec 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,6 +1,6 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? - if !resource[:replace] and count_matches(match_regex) > 0 + if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 true else lines.find do |line| @@ -10,7 +10,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do end def create - unless !resource[:replace] and count_matches(match_regex) > 0 + unless resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 if resource[:match] handle_create_with_match elsif resource[:after] -- cgit v1.2.3 From 9bacf14ca24283a94883523064603babcd7046d3 Mon Sep 17 00:00:00 2001 From: Johnson Earls Date: Thu, 6 Aug 2015 13:00:11 -0700 Subject: 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`). --- lib/puppet/provider/file_line/ruby.rb | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'lib/puppet/provider/file_line/ruby.rb') 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. # -- cgit v1.2.3 From bcab71ded8507de4fd9f89b4dcf798b3d98ace59 Mon Sep 17 00:00:00 2001 From: Hailee Kenney Date: Thu, 6 Oct 2016 15:23:35 -0700 Subject: (MODULES-3590) Fix match_for_absence parameter Prior to this commit, due to a bug in the exists? method in the file_line provider, match_for_absence didn't work as described (or at all really). Update the exists? logic so that match_for_absence works as described. Additionally add a unit test to prevent regressions and update the documentation for the parameter to reflect the fact that it is ignored when `ensure => present`. --- lib/puppet/provider/file_line/ruby.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/puppet/provider/file_line/ruby.rb') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index aab6fe2..beeb430 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -4,7 +4,11 @@ Puppet::Type.type(:file_line).provide(:ruby) do true else lines.find do |line| - line.chomp == resource[:line].chomp + if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' + line.chomp =~ Regexp.new(resource[:match]) + else + line.chomp == resource[:line].chomp + end end end end -- cgit v1.2.3