summaryrefslogtreecommitdiff
path: root/lib/puppet
diff options
context:
space:
mode:
authorStephen Benjamin <stephen@bitbin.de>2014-05-14 20:33:57 +0200
committerStephen Benjamin <stephen@bitbin.de>2014-05-14 20:33:57 +0200
commit6eaa592cd8d5af097a4624b3d1cead74408aabf2 (patch)
tree436a27d393345adb11b3d13a349257648b6f0813 /lib/puppet
parent08b00d9229961d7b3c3cba997bfb35c8d47e4c4b (diff)
downloadpuppet-stdlib-6eaa592cd8d5af097a4624b3d1cead74408aabf2.tar.gz
puppet-stdlib-6eaa592cd8d5af097a4624b3d1cead74408aabf2.tar.bz2
(PUP-2571) add 'before' functionality to file_line
file_line supports adding lines after a match, but there are use cases when having "before" would be useful. For example, in Debian-based OS's, the last line of /etc/rc.local is "exit 0" it's an incredible pain to deal with that scenario today. This commit adds a 'before' parameter to the file_line type, and implements it for the ruby provider.
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/provider/file_line/ruby.rb16
-rw-r--r--lib/puppet/type/file_line.rb4
2 files changed, 13 insertions, 7 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index 94e7fac..2cbd172 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -9,7 +9,9 @@ Puppet::Type.type(:file_line).provide(:ruby) do
if resource[:match]
handle_create_with_match
elsif resource[:after]
- handle_create_with_after
+ handle_create_with_position :after
+ elsif resource[:before]
+ handle_create_with_position :before
else
append_line
end
@@ -49,29 +51,29 @@ Puppet::Type.type(:file_line).provide(:ruby) do
end
end
- def handle_create_with_after
- regex = Regexp.new(resource[:after])
+ def handle_create_with_position(position)
+ regex = resource[position] ? Regexp.new(resource[position]) : nil
count = lines.count {|l| l.match(regex)}
case count
- when 1 # find the line to put our line after
+ when 1 # find the line to put our line before/after
File.open(resource[:path], 'w') do |fh|
lines.each do |l|
- fh.puts(l)
+ fh.puts(l) if position == :after
if regex.match(l) then
fh.puts(resource[:line])
end
+ fh.puts(l) if position == :before
end
end
when 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."
+ raise Puppet::Error, "#{count} lines match pattern '#{resource[position]}' in file '#{resource[:path]}'. One or no line must match the pattern."
end
end
- ##
# append the line to the file.
#
# @api private
diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb
index 323fc4c..bc6745f 100644
--- a/lib/puppet/type/file_line.rb
+++ b/lib/puppet/type/file_line.rb
@@ -46,6 +46,10 @@ Puppet::Type.newtype(:file_line) do
desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)'
end
+ newparam(:before) do
+ desc 'An optional value used to specify the line before which we will add any new lines. (Existing lines are added in place)'
+ end
+
newparam(:line) do
desc 'The line to be appended to the file located by the path parameter.'
end