diff options
-rw-r--r-- | README.markdown | 2 | ||||
-rw-r--r-- | lib/puppet/provider/file_line/ruby.rb | 6 | ||||
-rw-r--r-- | lib/puppet/type/file_line.rb | 3 | ||||
-rwxr-xr-x | spec/unit/puppet/provider/file_line/ruby_spec.rb | 7 |
4 files changed, 15 insertions, 3 deletions
diff --git a/README.markdown b/README.markdown index d27668b..34d26a8 100644 --- a/README.markdown +++ b/README.markdown @@ -128,7 +128,7 @@ All parameters are optional, unless otherwise noted. * `ensure`: Ensures whether the resource is present. Valid options: 'present', 'absent'. Default: 'present'. * `line`: **Required.** Sets the line to be added to the file located by the `path` parameter. Valid options: String. Default: Undefined. * `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. A regex comparison is performed against the line value, and if it does not match, an exception is raised. Valid options: String containing a regex. Default: Undefined. -* `match_for_absence`: An optional value to determine if match should be applied when `ensure => absent`. If set to true and match is set, the line that matches match will be deleted. If set to false (the default), match is ignored when `ensure => absent` and the value of `line` is used instead. Default: false. +* `match_for_absence`: An optional value to determine if match should be applied when `ensure => absent`. If set to true and match is set, the line that matches match will be deleted. If set to false (the default), match is ignored when `ensure => absent` and the value of `line` is used instead. Ignored when `ensure => present`. Default: false. * `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. * `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. * `path`: **Required.** Defines the file in which Puppet will ensure the line specified by `line`. Must be an absolute path to the file. diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index aab6fe2..beeb430 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -4,7 +4,11 @@ Puppet::Type.type(:file_line).provide(:ruby) do true else lines.find do |line| - line.chomp == resource[:line].chomp + if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' + line.chomp =~ Regexp.new(resource[:match]) + else + line.chomp == resource[:line].chomp + end end end end diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 6f5c188..7b7d44e 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -71,7 +71,8 @@ Puppet::Type.newtype(:file_line) do newparam(:match_for_absence) do desc 'An optional value to determine if match should be applied when ensure => absent.' + ' If set to true and match is set, the line that matches match will be deleted.' + - ' If set to false (the default), match is ignored when ensure => absent.' + ' If set to false (the default), match is ignored when ensure => absent.' + + ' When `ensure => present`, match_for_absence is ignored.' newvalues(true, false) defaultto false end diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index fdeaf1a..1f41f62 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -363,6 +363,13 @@ describe provider_class do @provider = provider_class.new(@resource) end + it 'should find a line to match' do + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo\nfoo2") + end + expect(@provider.exists?).to be_truthy + end + it 'should remove one line if it matches' do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2") |