Skip to content

Commit

Permalink
Modify in_position? method to fix inconsistencies
Browse files Browse the repository at this point in the history
in_position? method wasn't working right due to issues with using following-sibling when before and preceding-sibling when after, this corrects it with new, hopefully proper assumptions.  The only item that small change didn't fix was last() which was not working for either because of the way siblings treat the last() command as it will always resolve to true on the first match in those cases.  This patch assumes that you'll never have more than one [last()] in your xpath for positioning and swaps it out with the correct final array value of augeas matches.  We use an invert boolean on 'after' positions as we will always have an empty? resolving as true if the match exists before the item we're looking for.  Whereas we'll always have empty? as false if we do a 'before' and the actual item currently exists after the line we're trying to match.
  • Loading branch information
asasfu committed Nov 25, 2015
1 parent 7d2931f commit 0d631dc
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/puppet/provider/pam/augeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ def in_position?
unless resource[:position].nil?
path, before = self.class.position_path(resource[:position], resource[:type])

if before == 'before'
mpath = "#{resource_path}[following-sibling::#{path}]"
else
mpath = "#{resource_path}[preceding-sibling::#{path}]"
# Would someone ever have an xpath for positioning that would include more than one [last()] ?
# This section banks on there only ever being one [last()]...
# We are unable to use [last()] inside the preceding-sibling block as it would always result in [1] so we do an
# extra call to augeas to find the last value of the matches and subtract 1 to match the proper lookup/emulate last()
if path.include?('[last()]')
last_value = augopen.match(path)
return false if last_value.empty?
last_value = last_value[0].split('/').last.to_i - 1
path.sub!(/\[last\(\)\]/, "[#{last_value}]")
end
mpath = "#{resource_path}[preceding-sibling::#{path}]"

augmatch = false
augopen do |aug|
!aug.match(mpath).empty?
augmatch = aug.match(mpath).empty?
# Since we use preceding-sibling for everything, we invert on the result if 'after' instead as it's more consistent
before == 'before' ? augmatch : !augmatch
end
end
end
Expand Down

0 comments on commit 0d631dc

Please sign in to comment.