Skip to content

Commit 703fa77

Browse files
ripclawffbeputnam
authored andcommitted
Add new file_line option append_on_no_match
1 parent 596878a commit 703fa77

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ file_line { 'bashrc_proxy':
115115

116116
In the example above, `match` looks for a line beginning with 'export' followed by 'HTTP_PROXY' and replaces it with the value in line.
117117

118+
Match Example:
119+
120+
file_line { 'bashrc_proxy':
121+
ensure => present,
122+
path => '/etc/bashrc',
123+
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
124+
match => '^export\ HTTP_PROXY\=',
125+
append_on_no_match => false,
126+
}
127+
128+
In this code example, `match` looks for a line beginning with export followed by HTTP_PROXY and replaces it with the value in line. If a match is not found, then no changes are made to the file.
129+
118130
Match Example with `ensure => absent`:
119131

120132
```puppet

lib/puppet/provider/file_line/ruby.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def exists?
1212
found = lines_count > 0
1313
else
1414
match_count = count_matches(new_match_regex)
15-
if resource[:replace].to_s == 'true'
15+
if resource[:append_on_no_match].to_s == 'false'
16+
found = true
17+
elsif resource[:replace].to_s == 'true'
1618
found = lines_count > 0 && lines_count == match_count
1719
else
1820
found = match_count > 0

lib/puppet/type/file_line.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
encoding => "iso-8859-1",
5959
}
6060
61-
Files with special characters that are not valid UTF-8 will give the
61+
Files with special characters that are not valid UTF-8 will give the
6262
error message "invalid byte sequence in UTF-8". In this case, determine
6363
the correct file encoding and specify the correct encoding using the
6464
encoding attribute, the value of which needs to be a valid Ruby character
@@ -136,6 +136,12 @@ def retrieve
136136
defaultto 'UTF-8'
137137
end
138138

139+
newparam(:append_on_no_match) do
140+
desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
141+
newvalues(true, false)
142+
defaultto true
143+
end
144+
139145
# Autorequire the file resource if it's being managed
140146
autorequire(:file) do
141147
self[:path]

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@
194194
@provider.create
195195
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
196196
end
197+
198+
it 'should not add line after no matches found' do
199+
@resource = Puppet::Type::File_line.new(
200+
{
201+
:name => 'foo',
202+
:path => @tmpfile,
203+
:line => 'inserted = line',
204+
:match => '^foo3$',
205+
:append_on_no_match => false,
206+
}
207+
)
208+
@provider = provider_class.new(@resource)
209+
File.open(@tmpfile, 'w') do |fh|
210+
fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
211+
end
212+
expect(@provider.exists?).to be true
213+
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
214+
end
197215
end
198216

199217
describe 'using after' do

0 commit comments

Comments
 (0)