diff --git a/exe/scelint b/exe/scelint index 06b489c..fff9e89 100755 --- a/exe/scelint +++ b/exe/scelint @@ -1,38 +1,6 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'scelint' +require 'scelint/cli' -to_check = ARGV.empty? ? ['.'] : ARGV -lint = Scelint::Lint.new(to_check) - -count = lint.files.count - -if count.zero? - warn 'No SCE data found.' - exit 0 -end - -lint.errors.each do |error| - warn error -end - -lint.warnings.each do |warning| - warn warning -end - -message = "Checked #{count} files." -if lint.errors.count == 0 - message += ' No errors.' - exit_code = 0 -else - message += " #{lint.errors.count} errors." - exit_code = 1 -end - -if lint.warnings.count > 0 - message += " #{lint.warnings.count} warnings." -end - -puts message -exit exit_code +Scelint::CLI.start(['lint'] + ARGV) diff --git a/lib/scelint.rb b/lib/scelint.rb index 4a09ecf..3ad03c0 100644 --- a/lib/scelint.rb +++ b/lib/scelint.rb @@ -26,7 +26,7 @@ def initialize(paths = ['.']) merged_data = {} - paths.each do |path| + Array(paths).each do |path| if File.directory?(path) [ 'SIMP/compliance_profiles', diff --git a/lib/scelint/cli.rb b/lib/scelint/cli.rb new file mode 100644 index 0000000..4af712e --- /dev/null +++ b/lib/scelint/cli.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'scelint' +require 'thor' +require 'logger' + +# SCELint CLI +class Scelint::CLI < Thor + class_option :quiet, type: :boolean, aliases: '-q', default: false + class_option :verbose, type: :boolean, aliases: '-v', default: false + class_option :debug, type: :boolean, aliases: '-d', default: false + + desc 'lint PATH', 'Lint all files in PATH' + def lint(*paths) + paths = ['.'] if paths.nil? || paths.empty? + lint = Scelint::Lint.new(paths) + + count = lint.files.count + + if count.zero? + logger.error 'No SCE data found.' + exit 0 + end + + lint.errors.each do |error| + logger.error error + end + + lint.warnings.each do |warning| + logger.warn warning + end + + message = "Checked #{count} files." + if lint.errors.count == 0 + message += ' No errors.' + exit_code = 0 + else + message += " #{lint.errors.count} errors." + exit_code = 1 + end + + if lint.warnings.count > 0 + message += " #{lint.warnings.count} warnings." + end + + logger.info message + exit exit_code + rescue => e + logger.fatal e.message + end + default_task :lint + + private + + def logger + return @logger if @logger + @logger = Logger.new(STDOUT) + if options[:quiet] + @logger.level = Logger::FATAL + elsif options[:debug] + @logger.level = Logger::DEBUG + elsif options[:verbose] + @logger.level = Logger::INFO + else + @logger.level = Logger::INFO + @logger.formatter = proc do |_severity, _datetime, _progname, msg| + "#{msg}\n" + end + end + @logger + end +end diff --git a/scelint.gemspec b/scelint.gemspec index 6d999aa..121cf95 100644 --- a/scelint.gemspec +++ b/scelint.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |spec| spec.summary = %q{Linter SIMP Compliance Engine data} spec.homepage = 'https://github.com/simp/rubygem-simp-scelint' - spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0') + spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0') spec.metadata['homepage_uri'] = spec.homepage spec.metadata['source_code_uri'] = spec.homepage @@ -24,5 +24,6 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_runtime_dependency 'deep_merge' + spec.add_runtime_dependency 'deep_merge', '~> 1.2' + spec.add_runtime_dependency 'thor', '~> 1.3' end