Skip to content

Commit

Permalink
(simp#14) adds option parsing, output and failure levels, and validat…
Browse files Browse the repository at this point in the history
…ion pass skipping

closes simp#22, simp#23, simp#24, simp#25
  • Loading branch information
Rick Gardner authored and Rick Gardner committed Jan 19, 2023
1 parent 23698e1 commit 0b7cf75
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
40 changes: 34 additions & 6 deletions exe/scelint
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@
# frozen_string_literal: true

require 'scelint'
require 'optparse'

@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: scelint.rb [options] [modules_dir]"

@options[:output_level] = :error
opts.on("-o", "--output_level [OUTPUT_LEVEL]", [:error, :warning, :notes], "Level of output to display (error, warning, notes)") do |output|
@options[:output_level] = output
end

@options[:fail_on] = :error
opts.on("-f", "--fail_on_level [FAIL_LEVEL]", [:error, :warning ], "Level of output to fail on (error, warning)") do |fail_level|
@options[:fail_on] = fail_level
end

@options[:puppet_validation] == false
opts.on("-p", "--puppet_validation", "Run extensive puppet validation") do |p|
@options[:puppet_validation] = p
end
end.parse!

to_check = ARGV.empty? ? ['.'] : ARGV
lint = Scelint::Lint.new(to_check)
lint = Scelint::Lint.new(@options, to_check)

count = lint.files.count

Expand All @@ -17,18 +38,25 @@ lint.errors.each do |error|
warn "ERROR: #{error}"
end

lint.warnings.each do |warning|
warn "WARNING: #{warning}"
unless @options[:output_level] == :error
lint.warnings.each do |warning|
warn "WARNING: #{warning}"
end
end

lint.notes.each do |note|
warn "NOTE: #{note}"
unless @options[:output_level] == :error || @options[:output_level] == :warning
lint.notes.each do |note|
warn "NOTE: #{note}"
end
end

message = "Checked #{count} files."
if lint.errors.count == 0
if lint.errors.count == 0 && @options[:fail_on] != :warning
message += ' No errors.'
exit_code = 0
elsif lint.errors.count == 0
message += ' No errors.'
exit_code = 1
else
message += " #{lint.errors.count} errors."
exit_code = 1
Expand Down
15 changes: 9 additions & 6 deletions lib/scelint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'yaml'
require 'json'
require 'deep_merge'
require 'optparse'

require 'scelint/version'

Expand All @@ -17,7 +18,8 @@ class Error < StandardError; end
# @example Look for data in all modules in the current directory
# lint = Scelint::Lint.new(Dir.glob('*'))
class Lint
def initialize(paths = ['.'])
def initialize(options, paths = ['.'])
@options = options
@data = {}
@errors = []
@warnings = []
Expand Down Expand Up @@ -604,11 +606,12 @@ def validate
@notes << 'No profiles found, unable to validate Hiera data'
return nil
end

profiles.each do |profile|
compile(profile)
confines.each do |confine|
compile(profile, confine)
unless @options[:puppet_validation]
profiles.each do |profile|
compile(profile)
confines.each do |confine|
compile(profile, confine)
end
end
end
end
Expand Down

0 comments on commit 0b7cf75

Please sign in to comment.