Skip to content

Commit 4a8c0a5

Browse files
committed
Merge pull request #463 from CENGN/fix/master/file_line_multiple_after
(MODULES-2071) Patch file_line provider to use multiple with after
2 parents c9b810c + 72089f3 commit 4a8c0a5

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ All parameters are optional, unless otherwise noted.
9696
* `ensure`: Ensures whether the resource is present. Valid options: 'present', 'absent'. Default: 'present'.
9797
* `line`: **Required.** Sets the line to be added to the file located by the `path` parameter. Valid options: String. Default: Undefined.
9898
* `match`: Specifies a regular expression to run against existing lines in the file; if a match is found, it is replaced rather than adding a new line. Valid options: String containing a regex. Default: Undefined.
99-
* `multiple`: Determines if `match` can change multiple lines. If set to false, an exception will be raised if more than one line matches. Valid options: 'true', 'false'. Default: Undefined.
100-
* `name`: Sets the name to use as the identity of the resource. This is necessary if you want the resource namevar to differ from the supplied `title` of the resource. Valid options: String. Default: Undefined.
99+
* `multiple`: Determines if `match` and/or `after` can change multiple lines. If set to false, an exception will be raised if more than one line matches. Valid options: 'true', 'false'. Default: Undefined.
100+
* `name`: Sets the name to use as the identity of the resource. This is necessary if you want the resource namevar to differ from the supplied `title` of the resource. Valid options: String. Default: Undefined.
101101
* `path`: **Required.** Defines the file in which Puppet will ensure the line specified by `line`. Must be an absolute path to the file.
102102

103103

lib/puppet/provider/file_line/ruby.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,22 @@ def handle_create_with_match()
6161
def handle_create_with_after
6262
regex = Regexp.new(resource[:after])
6363
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
64+
65+
if count > 1 && resource[:multiple].to_s != 'true'
66+
raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern."
67+
end
68+
69+
File.open(resource[:path], 'w') do |fh|
70+
lines.each do |l|
71+
fh.puts(l)
72+
if regex.match(l) then
73+
fh.puts(resource[:line])
7274
end
7375
end
74-
when 0 # append the line to the end of the file
76+
end
77+
78+
if (count == 0) # append the line to the end of the file
7579
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."
7880
end
7981
end
8082

spec/unit/puppet/provider/file_line/ruby_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
end
202202
end
203203

204-
context 'with two lines matching the after expression' do
204+
context 'with multiple lines matching the after expression' do
205205
before :each do
206206
File.open(@tmpfile, 'w') do |fh|
207207
fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
@@ -211,6 +211,22 @@
211211
it 'errors out stating "One or no line must match the pattern"' do
212212
expect { provider.create }.to raise_error(Puppet::Error, /One or no line must match the pattern/)
213213
end
214+
215+
it 'adds the line after all lines matching the after expression' do
216+
@resource = Puppet::Type::File_line.new(
217+
{
218+
:name => 'foo',
219+
:path => @tmpfile,
220+
:line => 'inserted = line',
221+
:after => '^foo1$',
222+
:multiple => true,
223+
}
224+
)
225+
@provider = provider_class.new(@resource)
226+
expect(@provider.exists?).to be_nil
227+
@provider.create
228+
expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz")
229+
end
214230
end
215231

216232
context 'with no lines matching the after expression' do

0 commit comments

Comments
 (0)