Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CLI to use thor #31

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading