puppetlabs-stdlib/lib/puppet/provider/file_line/ruby.rb
Peter Meier fc5cfc8cca implement #11017 - make file_line type ensurable
* Implement a simple destroy method.
* Add tests for it
* Refactor code, so file is actually read only once. However, due
  to the nature how provider tests are run, we need to ensure that
  the file is read before we open it to write it.
2012-02-09 15:56:09 +01:00

27 lines
500 B
Ruby

Puppet::Type.type(:file_line).provide(:ruby) do
def exists?
lines.find do |line|
line.chomp == resource[:line].chomp
end
end
def create
File.open(resource[:path], 'a') do |fh|
fh.puts resource[:line]
end
end
def destroy
local_lines = lines
File.open(resource[:path],'w') do |fh|
fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join(''))
end
end
private
def lines
@lines ||= File.readlines(resource[:path])
end
end