1
1
Puppet ::Type . type ( :file_line ) . provide ( :ruby ) do
2
2
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
6
13
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
16
19
end
17
20
end
18
21
found
19
22
end
20
23
21
24
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
23
26
if resource [ :match ]
24
27
handle_create_with_match
25
28
elsif resource [ :after ]
26
29
handle_create_with_after
27
30
else
28
- append_line
31
+ handle_append_line
29
32
end
30
33
end
31
34
end
32
35
33
36
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 ]
35
38
handle_destroy_with_match
36
39
else
37
40
handle_destroy_line
38
41
end
39
42
end
40
43
41
44
private
45
+
42
46
def lines
43
47
# If this type is ever used with very large files, we should
44
48
# write this in a different way, using a temp
@@ -53,25 +57,34 @@ def lines
53
57
end
54
58
end
55
59
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
57
65
resource [ :match ] ? Regexp . new ( resource [ :match ] ) : nil
58
66
end
59
67
68
+ def count_matches ( regex )
69
+ lines . select { |line | line . match ( regex ) } . size
70
+ end
71
+
60
72
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 )
63
76
64
77
if match_count > 1 && resource [ :multiple ] . to_s != 'true'
65
78
raise Puppet ::Error , "More than one line in file '#{ resource [ :path ] } ' matches pattern '#{ resource [ :match ] } '"
66
79
end
67
80
68
81
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 )
73
86
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.
75
88
end
76
89
end
77
90
end
@@ -83,60 +96,53 @@ def handle_create_with_match()
83
96
end
84
97
85
98
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 )
88
101
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."
91
104
end
92
105
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 )
97
110
fh . puts ( resource [ :line ] )
98
111
end
99
112
end
100
- end
101
113
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
104
117
end
105
118
end
106
119
107
- def count_matches ( regex )
108
- lines . select { |l | l . match ( regex ) } . size
109
- end
110
-
111
120
def handle_destroy_with_match
121
+ match_regex = new_match_regex
112
122
match_count = count_matches ( match_regex )
113
123
if match_count > 1 && resource [ :multiple ] . to_s != 'true'
114
124
raise Puppet ::Error , "More than one line in file '#{ resource [ :path ] } ' matches pattern '#{ resource [ :match ] } '"
115
125
end
116
126
117
127
local_lines = lines
118
128
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 ( '' ) )
120
130
end
121
131
end
122
132
123
133
def handle_destroy_line
124
134
local_lines = lines
125
135
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 ( '' ) )
127
137
end
128
138
end
129
139
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 )
138
144
end
139
- fh . puts resource [ :line ]
145
+ fh . puts ( resource [ :line ] )
140
146
end
141
147
end
142
148
end
0 commit comments