Class: PuppetCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-check.rb

Overview

interfaces from CLI/tasks and to individual parsers

Defined Under Namespace

Classes: CLI, Tasks

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.clean_filesObject

Returns the value of attribute clean_files.



23
24
25
# File 'lib/puppet-check.rb', line 23

def clean_files
  @clean_files
end

.error_filesObject

Returns the value of attribute error_files.



23
24
25
# File 'lib/puppet-check.rb', line 23

def error_files
  @error_files
end

.future_parserObject

Returns the value of attribute future_parser.



23
24
25
# File 'lib/puppet-check.rb', line 23

def future_parser
  @future_parser
end

.ignored_filesObject

Returns the value of attribute ignored_files.



23
24
25
# File 'lib/puppet-check.rb', line 23

def ignored_files
  @ignored_files
end

.puppetlint_argsObject

Returns the value of attribute puppetlint_args.



23
24
25
# File 'lib/puppet-check.rb', line 23

def puppetlint_args
  @puppetlint_args
end

.rubocop_argsObject

Returns the value of attribute rubocop_args.



23
24
25
# File 'lib/puppet-check.rb', line 23

def rubocop_args
  @rubocop_args
end

.style_checkObject

Returns the value of attribute style_check.



23
24
25
# File 'lib/puppet-check.rb', line 23

def style_check
  @style_check
end

.warning_filesObject

Returns the value of attribute warning_files.



23
24
25
# File 'lib/puppet-check.rb', line 23

def warning_files
  @warning_files
end

Class Method Details

.output_resultsObject

output the results for the files that were requested to be checked



82
83
84
85
86
87
# File 'lib/puppet-check.rb', line 82

def self.output_results
  puts "\033[31mThe following files have errors:\033[0m", error_files.join("\n\n") unless error_files.empty?
  puts "\n\033[33mThe following files have warnings:\033[0m", warning_files.join("\n\n") unless warning_files.empty?
  puts "\n\033[32mThe following files have no errors or warnings:\033[0m", clean_files unless clean_files.empty?
  puts "\n\033[36mThe following files have unrecognized formats and therefore were not processed:\033[0m", ignored_files unless ignored_files.empty?
end

.parse_paths(paths) ⇒ Object

parse the paths and return the array of files



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet-check.rb', line 42

def self.parse_paths(paths)
  files = []

  # traverse the unique paths and return all files
  paths.uniq.each do |path|
    if File.directory?(path)
      files.concat(Dir.glob("#{path}/**/*").select { |subpath| File.file? subpath })
    elsif File.file?(path)
      files.push(path)
    end
  end

  # do not process fixtures, check that at least one file was found, and remove double slashes
  files.reject! { |file| file =~ /fixtures/ }
  raise "puppet-check: no files found in supplied paths #{paths.join(', ')}." if files.empty?
  files.map! { |file| file.gsub('//', '/') }

  files.uniq
end

Instance Method Details

#execute_parsers(files) ⇒ Object

categorize and pass the files out to the parsers to determine their status



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppet-check.rb', line 63

def execute_parsers(files)
  PuppetParser.manifest(files.select { |file| File.extname(file) == '.pp' })
  files.reject! { |file| File.extname(file) == '.pp' }
  PuppetParser.template(files.select { |file| File.extname(file) == '.epp' })
  files.reject! { |file| File.extname(file) == '.epp' }
  RubyParser.ruby(files.select { |file| File.extname(file) == '.rb' })
  files.reject! { |file| File.extname(file) == '.rb' }
  RubyParser.template(files.select { |file| File.extname(file) == '.erb' })
  files.reject! { |file| File.extname(file) == '.erb' }
  DataParser.yaml(files.select { |file| File.extname(file) =~ /\.ya?ml$/ })
  files.reject! { |file| File.extname(file) =~ /\.ya?ml$/ }
  DataParser.json(files.select { |file| File.extname(file) == '.json' })
  files.reject! { |file| File.extname(file) == '.json' }
  RubyParser.librarian(files.select { |file| File.basename(file) =~ /(?:Puppet|Module|Rake|Gem)file$/ })
  files.reject! { |file| File.basename(file) =~ /(?:Puppet|Module|Rake|Gem)file$/ }
  files.each { |file| self.class.ignored_files.push("-- #{file}") }
end

#run(paths) ⇒ Object

main runner for PuppetCheck



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet-check.rb', line 27

def run(paths)
  # grab all of the files to be processed
  files = self.class.parse_paths(paths)

  # parse the files
  execute_parsers(files)

  # output the diagnostic results
  self.class.output_results

  # exit code
  self.class.error_files.empty? ? 0 : 2
end