1 Puppet::Type.type(:file_line).provide(:ruby) do
6 found = line.chomp == resource[:line]
11 if resource[:match] == nil
12 found = lines_count > 0
14 match_count = count_matches(new_match_regex)
15 if resource[:append_on_no_match].to_s == 'false'
17 elsif resource[:replace].to_s == 'true'
18 found = lines_count > 0 && lines_count == match_count
20 found = match_count > 0
27 unless resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0
29 handle_create_with_match
30 elsif resource[:after]
31 handle_create_with_after
39 if resource[:match_for_absence].to_s == 'true' && resource[:match]
40 handle_destroy_with_match
49 # If this type is ever used with very large files, we should
50 # write this in a different way, using a temp
51 # file; for now assuming that this type is only used on
52 # small-ish config files that can fit into memory without
55 @lines ||= File.readlines(resource[:path], :encoding => resource[:encoding])
57 # Ruby 1.8 doesn't support open_args
58 @lines ||= File.readlines(resource[:path])
63 resource[:after] ? Regexp.new(resource[:after]) : nil
67 resource[:match] ? Regexp.new(resource[:match]) : nil
70 def count_matches(regex)
71 lines.select{ |line| line.match(regex) }.size
74 def handle_create_with_match()
75 after_regex = new_after_regex
76 match_regex = new_match_regex
77 match_count = count_matches(new_match_regex)
79 if match_count > 1 && resource[:multiple].to_s != 'true'
80 raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
83 File.open(resource[:path], 'w') do |fh|
85 fh.puts(match_regex.match(line) ? resource[:line] : line)
86 if match_count == 0 && after_regex
87 if after_regex.match(line)
88 fh.puts(resource[:line])
89 match_count += 1 # Increment match_count to indicate that the new line has been inserted.
95 fh.puts(resource[:line])
100 def handle_create_with_after
101 after_regex = new_after_regex
102 after_count = count_matches(after_regex)
104 if after_count > 1 && resource[:multiple].to_s != 'true'
105 raise Puppet::Error, "#{after_count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern."
108 File.open(resource[:path],'w') do |fh|
111 if after_regex.match(line)
112 fh.puts(resource[:line])
116 if (after_count == 0)
117 fh.puts(resource[:line])
122 def handle_destroy_with_match
123 match_regex = new_match_regex
124 match_count = count_matches(match_regex)
125 if match_count > 1 && resource[:multiple].to_s != 'true'
126 raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
130 File.open(resource[:path],'w') do |fh|
131 fh.write(local_lines.reject{ |line| match_regex.match(line) }.join(''))
135 def handle_destroy_line
137 File.open(resource[:path],'w') do |fh|
138 fh.write(local_lines.reject{ |line| line.chomp == resource[:line] }.join(''))
142 def handle_append_line
143 File.open(resource[:path],'w') do |fh|
147 fh.puts(resource[:line])