]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/provider/file_line/ruby.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / provider / file_line / ruby.rb
1 Puppet::Type.type(:file_line).provide(:ruby) do
2   def exists?
3     lines.find do |line|
4       line.chomp == resource[:line].chomp
5     end
6   end
7
8   def create
9     if resource[:match]
10       handle_create_with_match
11     elsif resource[:after]
12       handle_create_with_after
13     else
14       append_line
15     end
16   end
17
18   def destroy
19     local_lines = lines
20     File.open(resource[:path],'w') do |fh|
21       fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join(''))
22     end
23   end
24
25   private
26   def lines
27     # If this type is ever used with very large files, we should
28     #  write this in a different way, using a temp
29     #  file; for now assuming that this type is only used on
30     #  small-ish config files that can fit into memory without
31     #  too much trouble.
32     @lines ||= File.readlines(resource[:path])
33   end
34
35   def handle_create_with_match()
36     regex = resource[:match] ? Regexp.new(resource[:match]) : nil
37     regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil
38     match_count = count_matches(regex)
39
40     if match_count > 1 && resource[:multiple].to_s != 'true'
41      raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
42     end
43
44     File.open(resource[:path], 'w') do |fh|
45       lines.each do |l|
46         fh.puts(regex.match(l) ? resource[:line] : l)
47         if (match_count == 0 and regex_after)
48           if regex_after.match(l)
49             fh.puts(resource[:line])
50             match_count += 1 #Increment match_count to indicate that the new line has been inserted.
51           end
52         end
53       end
54
55       if (match_count == 0)
56         fh.puts(resource[:line])
57       end
58     end
59   end
60
61   def handle_create_with_after
62     regex = Regexp.new(resource[:after])
63     count = count_matches(regex)
64     case count
65     when 1 # find the line to put our line after
66       File.open(resource[:path], 'w') do |fh|
67         lines.each do |l|
68           fh.puts(l)
69           if regex.match(l) then
70             fh.puts(resource[:line])
71           end
72         end
73       end
74     when 0 # append the line to the end of the file
75       append_line
76     else
77       raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'.  One or no line must match the pattern."
78     end
79   end
80
81   def count_matches(regex)
82     lines.select{|l| l.match(regex)}.size
83   end
84
85   ##
86   # append the line to the file.
87   #
88   # @api private
89   def append_line
90     File.open(resource[:path], 'w') do |fh|
91       lines.each do |l|
92         fh.puts(l)
93       end
94       fh.puts resource[:line]
95     end
96   end
97 end