Skip to content

Commit

Permalink
Refactor CLI to use thor
Browse files Browse the repository at this point in the history
* Use `thor` gem for the CLI
* Add quiet/verbose/debug options to the CLI
* Bump minimum required Ruby version to 2.7.0 (oldest version we're
  testing with)
* Accept `String` arguments to `new()`

- Fixes #23
- Fixes #24
  • Loading branch information
silug committed Oct 28, 2024
1 parent fb8378d commit ac3b7ff
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 37 deletions.
36 changes: 2 additions & 34 deletions exe/scelint
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion lib/scelint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
72 changes: 72 additions & 0 deletions lib/scelint/cli.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions scelint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit ac3b7ff

Please sign in to comment.