summaryrefslogtreecommitdiff
path: root/lib/puppet/provider
diff options
context:
space:
mode:
authorDan Prince <dprince@redhat.com>2013-08-29 12:19:16 -0400
committerJeff McCune <jeff@puppetlabs.com>2013-08-29 15:07:29 -0700
commit221277e8522b42bf170fded6ea23dfc526703b07 (patch)
tree51abab0793eb3ef67431889330c2e7b28383f1d1 /lib/puppet/provider
parent806430224ad0da860be3761ab83f1e574b64fc60 (diff)
downloadpuppet-stdlib-221277e8522b42bf170fded6ea23dfc526703b07.tar.gz
puppet-stdlib-221277e8522b42bf170fded6ea23dfc526703b07.tar.bz2
Update file_line resource to support 'after'.
When adding new lines to a file the 'after' option can be useful when you need to insert file lines into the middle of a file. This is particularly helpful when using file_line with sectioned config files. NOTE: the after option only works when adding new lines. If you are updating an existing (matched) line it will simply modify it in place. This assumes it was in the right place to begin with.
Diffstat (limited to 'lib/puppet/provider')
-rw-r--r--lib/puppet/provider/file_line/ruby.rb28
1 files changed, 25 insertions, 3 deletions
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index 3cb9f6e..7ddb39f 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -49,10 +49,32 @@ Puppet::Type.type(:file_line).provide(:ruby) do
end
def handle_create_without_match
- File.open(resource[:path], 'a') do |fh|
- fh.puts resource[:line]
+
+ regex = resource[:after] ? Regexp.new(resource[:after]) : nil
+ after_count = File.exists?(resource[:path]) ? lines.select { |l| regex.match(l) }.size : 0
+ if after_count > 1 then
+ raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches after pattern '#{resource[:after]}'"
end
- end
+ if (after_count == 0)
+
+ File.open(resource[:path], 'a') do |fh|
+ fh.puts resource[:line]
+ end
+
+ else
+
+ 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
+ end
+
+ end
+
+ end
end