From 0b7cf7569cdfa88de7ff080aa71174b9a632cbcf Mon Sep 17 00:00:00 2001 From: Rick Gardner Date: Thu, 19 Jan 2023 11:57:33 -0500 Subject: [PATCH] (#14) adds option parsing, output and failure levels, and validation pass skipping closes #22, #23, #24, #25 --- exe/scelint | 40 ++++++++++++++++++++++++++++++++++------ lib/scelint.rb | 15 +++++++++------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/exe/scelint b/exe/scelint index 87df9bd..4747973 100755 --- a/exe/scelint +++ b/exe/scelint @@ -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 @@ -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 diff --git a/lib/scelint.rb b/lib/scelint.rb index 6260c06..3405f88 100644 --- a/lib/scelint.rb +++ b/lib/scelint.rb @@ -3,6 +3,7 @@ require 'yaml' require 'json' require 'deep_merge' +require 'optparse' require 'scelint/version' @@ -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 = [] @@ -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