Sha256: e3864871563a29096cd3df9a488b3144fe6b24b15c10d3944a8cdfc009ea10ae

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'puppet-ghostbuster/util'

class PuppetLint::Checks
  def load_data(path, content)
    lexer = PuppetLint::Lexer.new
    PuppetLint::Data.path = path
    begin
      PuppetLint::Data.manifest_lines = content.split("\n", -1)
      PuppetLint::Data.tokens = lexer.tokenise(content)
      PuppetLint::Data.parse_control_comments
    rescue StandardError
      PuppetLint::Data.tokens = []
    end
  end
end

PuppetLint.new_check(:ghostbuster_facts) do
  def manifests
    Dir.glob('./**/manifests/**/*.pp')
  end

  def templates
    Dir.glob('./**/templates/**/*').select { |f| File.file? f }
  end

  def check
    m = path.match(%r{.*/([^/]+)/lib/facter/(.+)$})
    return if m.nil?

    File.foreach(path) do |line|
      if line =~ /Facter.add\(["':](?<fact>[^"'\)]+)["']?\)/
        fact_name = Regexp.last_match(:fact)

        found = false

        manifests.each do |manifest|
          found = true unless PuppetGhostbuster::Util.search_file(manifest, /(\$\{?::#{fact_name}\}?|@#{fact_name})/).nil?
          break if found
        end

        templates.each do |template|
          found = true unless PuppetGhostbuster::Util.search_file(template, /@#{fact_name}/).nil?
          break if found
        end

        next if found

        notify :warning, {
          message: "Fact #{fact_name} seems unused",
          line: 1,
          column: 1,
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
puppet-ghostbuster-1.2.1 lib/puppet-lint/plugins/check_ghostbuster_facts.rb
puppet-ghostbuster-1.2.0 lib/puppet-lint/plugins/check_ghostbuster_facts.rb