Skip to content

Commit

Permalink
(GH-189) - invalid fact correction for top scope structured fact
Browse files Browse the repository at this point in the history
Prior to this commit, if you had a top scope structured fact like this
in your manifest `$::my_structured_fact['foo']['test']`, lint would
return an invalid correction when passed the fix flag.

Now, we have altered the top_scope_facts plugin, to first check if it is
a structured fact, and if one is found using regex, apply different
logic to fix.
  • Loading branch information
jordanbreen28 committed Feb 5, 2024
1 parent 6d5b00e commit eef5b0a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/puppet-lint/plugins/top_scope_facts/top_scope_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def check
end

def fix(problem)
problem[:token].value = "facts['" + problem[:token].value.sub(%r{^::}, '') + "']"
problem[:token].value = problem[:token].value.sub(%r{^::}, '')
# checks if the fact is a top level structured fact fact e.g. ::my_structured_fact['foo']['bar']
if %r{\[.*}.match?(problem[:token].value)
top_scope_variable = problem[:token].value.sub(%r{\[.*}, '')
structured_facts = problem[:token].value.scan(%r{\[.+}).first
problem[:token].value = "facts['" + top_scope_variable + "']" + structured_facts
problem[:token].value.sub!('"', '\'')
else
problem[:token].value = "facts['" + problem[:token].value + "']"
end
end
end

0 comments on commit eef5b0a

Please # to comment.