Skip to content

Commit

Permalink
extended legacy facts check to .yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
alex501212 committed Feb 18, 2025
1 parent 002f275 commit e234ad6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
11 changes: 7 additions & 4 deletions lib/puppet-lint/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ def run

path = path.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
path = if File.directory?(path)
Dir.glob("#{path}/**/*.pp")
else
@args
end
Dir.glob([

Check failure on line 69 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Layout/IndentationWidth: Use 2 (not -5) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

Check failure on line 69 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/IndentationWidth: Use 2 (not -5) spaces for indentation. (https://rubystyle.guide#spaces-indentation)

Check failure on line 69 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/IndentationWidth: Use 2 (not -5) spaces for indentation. (https://rubystyle.guide#spaces-indentation)
"#{path}/**/*.pp",

Check failure on line 70 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.

Check failure on line 70 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.

Check failure on line 70 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.
"#{path}/**/*.{yaml,yml}"

Check failure on line 71 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array. (https://rubystyle.guide#no-trailing-array-commas)

Check failure on line 71 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array. (https://rubystyle.guide#no-trailing-array-commas)

Check failure on line 71 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array. (https://rubystyle.guide#no-trailing-array-commas)
])

Check failure on line 72 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Layout/FirstArrayElementIndentation: Indent the right bracket the same as the first position after the preceding left parenthesis.

Check failure on line 72 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/FirstArrayElementIndentation: Indent the right bracket the same as the first position after the preceding left parenthesis.

Check failure on line 72 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/FirstArrayElementIndentation: Indent the right bracket the same as the first position after the preceding left parenthesis.
else

Check failure on line 73 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Layout/ElseAlignment: Align `else` with `if`.

Check failure on line 73 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/ElseAlignment: Align `else` with `if`.

Check failure on line 73 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/ElseAlignment: Align `else` with `if`.
@args
end

Check failure on line 75 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Layout/EndAlignment: `end` at 75, 6 is not aligned with `if` at 68, 13.

Check failure on line 75 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Layout/EndAlignment: `end` at 75, 6 is not aligned with `if` at 68, 13.

Check failure on line 75 in lib/puppet-lint/bin.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Layout/EndAlignment: `end` at 75, 6 is not aligned with `if` at 68, 13.

PuppetLint.configuration.with_filename = true if path.length > 1

Expand Down
26 changes: 18 additions & 8 deletions lib/puppet-lint/checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class PuppetLint::Checks
# Public: Get an Array of problem Hashes.
attr_accessor :problems

YAML_COMPATIBLE_CHECKS = [:legacy_facts].freeze

# Public: Initialise a new PuppetLint::Checks object.
def initialize
@problems = []
Expand Down Expand Up @@ -45,23 +47,31 @@ def load_data(path, content)
end
end

# Internal: Run the lint checks over the manifest code.
# Internal: Run the lint checks over the manifest or YAML code.
#
# fileinfo - The path to the file as passed to puppet-lint as a String.
# data - The String manifest code to be checked.
# data - The String manifest or YAML code to be checked.
#
# Returns an Array of problem Hashes.
def run(fileinfo, data)
load_data(fileinfo, data)

checks_run = []
enabled_checks.each do |check|
klass = PuppetLint.configuration.check_object[check].new
# FIXME: shadowing #problems
problems = klass.run
checks_run << [klass, problems]
if File.extname(fileinfo).downcase.match?(/\.ya?ml$/)

Check failure on line 60 in lib/puppet-lint/checks.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)

Check failure on line 60 in lib/puppet-lint/checks.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)

Check failure on line 60 in lib/puppet-lint/checks.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)
enabled_checks.select { |check| YAML_COMPATIBLE_CHECKS.include?(check) }.each do |check|
klass = PuppetLint.configuration.check_object[check].new
# FIXME: shadowing #problems
problems = klass.run
checks_run << [klass, problems]
end
else
enabled_checks.each do |check|
klass = PuppetLint.configuration.check_object[check].new
# FIXME: shadowing #problems
problems = klass.run
checks_run << [klass, problems]
end
end

checks_run.each do |klass, problems|
if PuppetLint.configuration.fix
@problems.concat(klass.fix_problems)
Expand Down
51 changes: 51 additions & 0 deletions lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'yaml'

# Public: A puppet-lint custom check to detect legacy facts.
#
# This check will optionally convert from legacy facts like $::operatingsystem
Expand Down Expand Up @@ -112,6 +114,55 @@

PuppetLint.new_check(:legacy_facts) do
def check
if File.extname(PuppetLint::Data.path).downcase.match?(/\.ya?ml$/)

Check failure on line 117 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)

Check failure on line 117 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)

Check failure on line 117 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)
begin
content = PuppetLint::Data.manifest_lines
yaml_content = content.join("\n")
data = YAML.safe_load(yaml_content, aliases: true, permitted_classes: [Symbol])
search_yaml(data)
rescue => e

Check failure on line 123 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Lint/UselessRescue: Useless `rescue` detected.

Check failure on line 123 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.3) / spec

Style/RescueStandardError: Avoid rescuing without specifying an error class.

Check failure on line 123 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Lint/UselessRescue: Useless `rescue` detected.

Check failure on line 123 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 2.7) / spec

Style/RescueStandardError: Avoid rescuing without specifying an error class.

Check failure on line 123 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Lint/UselessRescue: Useless `rescue` detected.

Check failure on line 123 in lib/puppet-lint/plugins/legacy_facts/legacy_facts.rb

View workflow job for this annotation

GitHub Actions / spec (ruby 3.2) / spec

Style/RescueStandardError: Avoid rescuing without specifying an error class.
raise e
end
else
check_puppet
end
end

def search_yaml(data, path = [])
case data
when Hash
data.each do |k, v|
search_value(k.to_s, path)
search_yaml(v, path + [k.to_s])
end
when Array
data.each_with_index { |v, i| search_yaml(v, path + [i]) }
when String
search_value(data, path)
end
end

def search_value(value, path)
value.scan(/%{(?:(?:::?)?|facts\.)([a-zA-Z0-9_]+)(?!\.[a-zA-Z])}/) do |match|
base_fact = match[0].split('.').first
next unless EASY_FACTS.include?(base_fact) || UNCONVERTIBLE_FACTS.include?(base_fact) || base_fact.match(Regexp.union(REGEX_FACTS))

notify :warning, {
message: "legacy fact '#{base_fact}'",
line: find_line_for_content(value),
column: 1
}
end
end

def find_line_for_content(content)
PuppetLint::Data.manifest_lines.each_with_index do |line, index|
return index + 1 if line.include?(content)
end
1
end

def check_puppet
tokens.select { |x| LEGACY_FACTS_VAR_TYPES.include?(x.type) }.each do |token|
fact_name = ''

Expand Down

0 comments on commit e234ad6

Please sign in to comment.