11Puppet ::Type . type ( :file_line ) . provide ( :ruby ) do
22 def exists?
3- found = true
4- if resource [ :replace ] . to_s != 'true' and count_matches ( match_regex ) > 0
5- found = true
3+ found = false
4+ lines_count = 0
5+ lines . each do |line |
6+ found = line . chomp == resource [ :line ]
7+ if found
8+ lines_count += 1
9+ end
10+ end
11+ if resource [ :match ] == nil
12+ found = lines_count > 0
613 else
7- lines . find do |line |
8- if resource [ :ensure ] . to_s == 'absent' and resource [ :match_for_absence ] . to_s == 'true'
9- found = line . chomp =~ Regexp . new ( resource [ :match ] )
10- else
11- found = line . chomp == resource [ :line ] . chomp
12- end
13- if found == false then
14- break
15- end
14+ match_count = count_matches ( new_match_regex )
15+ if resource [ :replace ] . to_s == 'true'
16+ found = lines_count > 0 && lines_count == match_count
17+ else
18+ found = match_count > 0
1619 end
1720 end
1821 found
1922 end
2023
2124 def create
22- unless resource [ :replace ] . to_s != 'true' and count_matches ( match_regex ) > 0
25+ unless resource [ :replace ] . to_s != 'true' && count_matches ( new_match_regex ) > 0
2326 if resource [ :match ]
2427 handle_create_with_match
2528 elsif resource [ :after ]
2629 handle_create_with_after
2730 else
28- append_line
31+ handle_append_line
2932 end
3033 end
3134 end
3235
3336 def destroy
34- if resource [ :match_for_absence ] . to_s == 'true' and resource [ :match ]
37+ if resource [ :match_for_absence ] . to_s == 'true' && resource [ :match ]
3538 handle_destroy_with_match
3639 else
3740 handle_destroy_line
3841 end
3942 end
4043
4144 private
45+
4246 def lines
4347 # If this type is ever used with very large files, we should
4448 # write this in a different way, using a temp
@@ -53,25 +57,34 @@ def lines
5357 end
5458 end
5559
56- def match_regex
60+ def new_after_regex
61+ resource [ :after ] ? Regexp . new ( resource [ :after ] ) : nil
62+ end
63+
64+ def new_match_regex
5765 resource [ :match ] ? Regexp . new ( resource [ :match ] ) : nil
5866 end
5967
68+ def count_matches ( regex )
69+ lines . select { |line | line . match ( regex ) } . size
70+ end
71+
6072 def handle_create_with_match ( )
61- regex_after = resource [ :after ] ? Regexp . new ( resource [ :after ] ) : nil
62- match_count = count_matches ( match_regex )
73+ after_regex = new_after_regex
74+ match_regex = new_match_regex
75+ match_count = count_matches ( new_match_regex )
6376
6477 if match_count > 1 && resource [ :multiple ] . to_s != 'true'
6578 raise Puppet ::Error , "More than one line in file '#{ resource [ :path ] } ' matches pattern '#{ resource [ :match ] } '"
6679 end
6780
6881 File . open ( resource [ :path ] , 'w' ) do |fh |
69- lines . each do |l |
70- fh . puts ( match_regex . match ( l ) ? resource [ :line ] : l )
71- if ( match_count == 0 and regex_after )
72- if regex_after . match ( l )
82+ lines . each do |line |
83+ fh . puts ( match_regex . match ( line ) ? resource [ :line ] : line )
84+ if match_count == 0 && after_regex
85+ if after_regex . match ( line )
7386 fh . puts ( resource [ :line ] )
74- match_count += 1 #Increment match_count to indicate that the new line has been inserted.
87+ match_count += 1 # Increment match_count to indicate that the new line has been inserted.
7588 end
7689 end
7790 end
@@ -83,60 +96,53 @@ def handle_create_with_match()
8396 end
8497
8598 def handle_create_with_after
86- regex = Regexp . new ( resource [ :after ] )
87- count = count_matches ( regex )
99+ after_regex = new_after_regex
100+ after_count = count_matches ( after_regex )
88101
89- if count > 1 && resource [ :multiple ] . to_s != 'true'
90- raise Puppet ::Error , "#{ count } lines match pattern '#{ resource [ :after ] } ' in file '#{ resource [ :path ] } '. One or no line must match the pattern."
102+ if after_count > 1 && resource [ :multiple ] . to_s != 'true'
103+ raise Puppet ::Error , "#{ after_count } lines match pattern '#{ resource [ :after ] } ' in file '#{ resource [ :path ] } '. One or no line must match the pattern."
91104 end
92105
93- File . open ( resource [ :path ] , 'w' ) do |fh |
94- lines . each do |l |
95- fh . puts ( l )
96- if regex . match ( l ) then
106+ File . open ( resource [ :path ] , 'w' ) do |fh |
107+ lines . each do |line |
108+ fh . puts ( line )
109+ if after_regex . match ( line )
97110 fh . puts ( resource [ :line ] )
98111 end
99112 end
100- end
101113
102- if ( count == 0 ) # append the line to the end of the file
103- append_line
114+ if ( after_count == 0 )
115+ fh . puts ( resource [ :line ] )
116+ end
104117 end
105118 end
106119
107- def count_matches ( regex )
108- lines . select { |l | l . match ( regex ) } . size
109- end
110-
111120 def handle_destroy_with_match
121+ match_regex = new_match_regex
112122 match_count = count_matches ( match_regex )
113123 if match_count > 1 && resource [ :multiple ] . to_s != 'true'
114124 raise Puppet ::Error , "More than one line in file '#{ resource [ :path ] } ' matches pattern '#{ resource [ :match ] } '"
115125 end
116126
117127 local_lines = lines
118128 File . open ( resource [ :path ] , 'w' ) do |fh |
119- fh . write ( local_lines . reject { | l | match_regex . match ( l ) } . join ( '' ) )
129+ fh . write ( local_lines . reject { | line | match_regex . match ( line ) } . join ( '' ) )
120130 end
121131 end
122132
123133 def handle_destroy_line
124134 local_lines = lines
125135 File . open ( resource [ :path ] , 'w' ) do |fh |
126- fh . write ( local_lines . reject { | l | l . chomp == resource [ :line ] } . join ( '' ) )
136+ fh . write ( local_lines . reject { | line | line . chomp == resource [ :line ] } . join ( '' ) )
127137 end
128138 end
129139
130- ##
131- # append the line to the file.
132- #
133- # @api private
134- def append_line
135- File . open ( resource [ :path ] , 'w' ) do |fh |
136- lines . each do |l |
137- fh . puts ( l )
140+ def handle_append_line
141+ File . open ( resource [ :path ] , 'w' ) do |fh |
142+ lines . each do |line |
143+ fh . puts ( line )
138144 end
139- fh . puts resource [ :line ]
145+ fh . puts ( resource [ :line ] )
140146 end
141147 end
142148end
0 commit comments