Skip to content

Commit 310fb93

Browse files
committed
Add new file_line option append_on_no_match
1 parent db8c1fb commit 310fb93

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

README.markdown

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ Match Example:
104104

105105
In this code example, `match` looks for a line beginning with export followed by HTTP_PROXY and replaces it with the value in line.
106106

107+
Match Example:
108+
109+
file_line { 'bashrc_proxy':
110+
ensure => present,
111+
path => '/etc/bashrc',
112+
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
113+
match => '^export\ HTTP_PROXY\=',
114+
append_on_no_match => false,
115+
}
116+
117+
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.
118+
107119
Match Example With `ensure => absent`:
108120

109121
file_line { 'bashrc_proxy':
@@ -1526,7 +1538,7 @@ The deprecation messages you get can vary, depending on the modules and data tha
15261538

15271539
The `validate_legacy` function helps you move from Puppet 3 style validation to Puppet 4 validation without breaking functionality your module's users depend on.
15281540

1529-
Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics.
1541+
Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics.
15301542

15311543
For each parameter of your classes and defined types, choose a new Puppet 4 data type to use. In most cases, the new data type allows a different set of values than the original `validate_*` function. The situation then looks like this:
15321544

lib/puppet/provider/file_line/ruby.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
def exists?
33
if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0
44
true
5+
elsif resource[:append_on_no_match].to_s == 'false' and count_matches(match_regex) == 0
6+
true
57
else
68
lines.find do |line|
79
if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true'

lib/puppet/type/file_line.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@
107107
defaultto true
108108
end
109109

110+
newparam(:append_on_no_match) do
111+
desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
112+
newvalues(true, false)
113+
defaultto true
114+
end
115+
110116
# Autorequire the file resource if it's being managed
111117
autorequire(:file) do
112118
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
@@ -174,6 +174,24 @@
174174
@provider.create
175175
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
176176
end
177+
178+
it 'should not add line after no matches found' do
179+
@resource = Puppet::Type::File_line.new(
180+
{
181+
:name => 'foo',
182+
:path => @tmpfile,
183+
:line => 'inserted = line',
184+
:match => '^foo3$',
185+
:append_on_no_match => false,
186+
}
187+
)
188+
@provider = provider_class.new(@resource)
189+
File.open(@tmpfile, 'w') do |fh|
190+
fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
191+
end
192+
expect(@provider.exists?).to be true
193+
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
194+
end
177195
end
178196

179197
describe 'using after' do

0 commit comments

Comments
 (0)