Skip to content

Commit

Permalink
[Changed] Updated code to support newest Thor gem
Browse files Browse the repository at this point in the history
  • Loading branch information
xtreme-shane-lattanzio committed Mar 1, 2022
1 parent 74cfa9f commit b118772
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/license_finder/cli/approvals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def add(*names)
assert_some names
modifying { names.each { |name| decisions.approve(name, txn) } }

say "The #{names.join(', ')} dependency has been approved!", :green
printer.say "The #{names.join(', ')} dependency has been approved!", :green
end

auditable
desc 'remove DEPENDENCY', 'Unapprove a dependency'
def remove(dep)
modifying { decisions.unapprove(dep, txn) }

say "The dependency #{dep} no longer has a manual approval"
printer.say "The dependency #{dep} no longer has a manual approval"
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/license_finder/cli/base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'thor'

module LicenseFinder
module CLI
class Base < Thor
Expand All @@ -24,12 +23,16 @@ def decisions
def config
@config ||= Configuration.with_optional_saved_config(license_finder_config)
end

def printer
@printer || Printer.new
end
end

private

def fail(message)
say(message) && exit(1)
printer.say(message) && exit(1)
end

def license_finder_config
Expand Down Expand Up @@ -85,10 +88,10 @@ def logger_mode
def say_each(coll)
if coll.any?
coll.each do |item|
say(block_given? ? yield(item) : item)
printer.say(block_given? ? yield(item) : item)
end
else
say '(none)'
printer.say '(none)'
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/license_finder/cli/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def add(name, license, version)
decisions.approve(name, txn) if options[:approve]
end
if options[:approve]
say "The #{name} dependency has been added and approved!", :green
printer.say "The #{name} dependency has been added and approved!", :green
else
say "The #{name} dependency has been added!", :green
printer.say "The #{name} dependency has been added!", :green
end
end

Expand All @@ -31,12 +31,12 @@ def add(name, license, version)
def remove(name)
modifying { decisions.remove_package(name, txn) }

say "The #{name} dependency has been removed.", :green
printer.say "The #{name} dependency has been removed.", :green
end

desc 'list', 'List manually added dependencies'
def list
say 'Manually Added Dependencies:', :blue
printer.say 'Manually Added Dependencies:', :blue
say_each(decisions.packages, &:name)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/license_finder/cli/ignored_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class IgnoredDependencies < Base

desc 'list', 'List all the ignored dependencies'
def list
say 'Ignored Dependencies:', :blue
printer.say 'Ignored Dependencies:', :blue
say_each(decisions.ignored)
end

Expand All @@ -17,15 +17,15 @@ def list
def add(dep)
modifying { decisions.ignore(dep, txn) }

say "Added #{dep} to the ignored dependencies"
printer.say "Added #{dep} to the ignored dependencies"
end

auditable
desc 'remove DEPENDENCY', 'Remove a dependency from the ignored dependencies'
def remove(dep)
modifying { decisions.heed(dep, txn) }

say "Removed #{dep} from the ignored dependencies"
printer.say "Removed #{dep} from the ignored dependencies"
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/license_finder/cli/ignored_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class IgnoredGroups < Base

desc 'list', 'List all the ignored groups'
def list
say 'Ignored Groups:', :blue
printer.say 'Ignored Groups:', :blue
say_each(decisions.ignored_groups)
end

Expand All @@ -17,15 +17,15 @@ def list
def add(group)
modifying { decisions.ignore_group(group, txn) }

say "Added #{group} to the ignored groups"
printer.say "Added #{group} to the ignored groups"
end

auditable
desc 'remove GROUP', 'Remove a group from the ignored groups'
def remove(group)
modifying { decisions.heed_group(group, txn) }

say "Removed #{group} from the ignored groups"
printer.say "Removed #{group} from the ignored groups"
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/license_finder/cli/inherited_decisions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class InheritedDecisions < Base

desc 'list', 'List all the inherited decision files'
def list
say 'Inherited Decision Files:', :blue
printer.say 'Inherited Decision Files:', :blue
say_each(decisions.inherited_decisions)
end

Expand All @@ -17,7 +17,7 @@ def list
def add(*decision_files)
assert_some decision_files
modifying { decision_files.each { |filepath| decisions.inherit_from(filepath) } }
say "Added #{decision_files.join(', ')} to the inherited decisions"
printer.say "Added #{decision_files.join(', ')} to the inherited decisions"
end

auditable
Expand All @@ -26,15 +26,15 @@ def add_with_auth(*params)
url, auth_type, token_or_env = params
auth_info = { 'url' => url, 'authorization' => "#{auth_type} #{token_or_env}" }
modifying { decisions.add_decision [:inherit_from, auth_info] }
say "Added #{url} to the inherited decisions"
printer.say "Added #{url} to the inherited decisions"
end

auditable
desc 'remove DECISION_FILE...', 'Remove one or more decision files from the inherited decisions'
def remove(*decision_files)
assert_some decision_files
modifying { decision_files.each { |filepath| decisions.remove_inheritance(filepath) } }
say "Removed #{decision_files.join(', ')} from the inherited decisions"
printer.say "Removed #{decision_files.join(', ')} from the inherited decisions"
end

auditable
Expand All @@ -43,7 +43,7 @@ def remove_with_auth(*params)
url, auth_type, token_or_env = params
auth_info = { 'url' => url, 'authorization' => "#{auth_type} #{token_or_env}" }
modifying { decisions.remove_inheritance(auth_info) }
say "Removed #{url} from the inherited decisions"
printer.say "Removed #{url} from the inherited decisions"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/license_finder/cli/licenses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class Licenses < Base
def add(name, license)
modifying { decisions.license(name, license, txn) }

say "The #{name} dependency has been marked as using #{license} license!", :green
printer.say "The #{name} dependency has been marked as using #{license} license!", :green
end

auditable
desc 'remove DEPENDENCY LICENSE', 'Remove a manually set license'
def remove(dep, lic)
modifying { decisions.unlicense(dep, lic, txn) }

say "The dependency #{dep} no longer has a manual license"
printer.say "The dependency #{dep} no longer has a manual license"
end
end
end
Expand Down
21 changes: 11 additions & 10 deletions lib/license_finder/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'license_finder/license_aggregator'
require 'license_finder/project_finder'
require 'license_finder/logger'
require 'license_finder/printer'

module LicenseFinder
module CLI
Expand Down Expand Up @@ -107,7 +108,7 @@ def project_roots

filtered_project_roots << project_path if aggregate_paths.include?(project_path) && !filtered_project_roots.include?(project_path)

say(filtered_project_roots)
printer.say(filtered_project_roots)
end

desc 'action_items', 'List unapproved dependencies (the default action for `license_finder`)'
Expand All @@ -120,25 +121,25 @@ def action_items
restricted = finder.restricted

# Ensure to start output on a new line even with dot progress indicators.
say "\n"
printer.say "\n"

unless any_packages
say 'No dependencies recognized!', :red
printer.say 'No dependencies recognized!', :red
exit 0
end

if unapproved.empty?
say 'All dependencies are approved for use', :green
printer.say 'All dependencies are approved for use', :green
else
unless restricted.empty?
say 'Restricted dependencies:', :red
say report_of(restricted)
printer.say 'Restricted dependencies:', :red
printer.say report_of(restricted)
end

other_unapproved = unapproved - restricted
unless other_unapproved.empty?
say 'Dependencies that need approval:', :yellow
say report_of(other_unapproved)
printer.say 'Dependencies that need approval:', :yellow
printer.say report_of(other_unapproved)
end

exit 1
Expand All @@ -156,7 +157,7 @@ def action_items
def report
finder = LicenseAggregator.new(config, aggregate_paths)
report = report_of(finder.dependencies)
save? ? save_report(report, config.save_file) : say(report)
save? ? save_report(report, config.save_file) : printer.say(report)
end

desc 'version', 'Print the version of LicenseFinder'
Expand All @@ -171,7 +172,7 @@ def diff(file1, file2)
f1 = IO.read(file1)
f2 = IO.read(file2)
report = DiffReport.new(Diff.compare(f1, f2))
save? ? save_report(report, config.save_file) : say(report)
save? ? save_report(report, config.save_file) : printer.say(report)
end

subcommand 'dependencies', Dependencies, 'Add or remove dependencies that your package managers are not aware of'
Expand Down
6 changes: 3 additions & 3 deletions lib/license_finder/cli/permitted_licenses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PermittedLicenses < Base

desc 'list', 'List all the permitted licenses'
def list
say 'Permitted Licenses:', :blue
printer.say 'Permitted Licenses:', :blue
say_each(decisions.permitted, &:name)
end

Expand All @@ -17,15 +17,15 @@ def list
def add(*licenses)
assert_some licenses
modifying { licenses.each { |l| decisions.permit(l, txn) } }
say "Added #{licenses.join(', ')} to the permitted licenses"
printer.say "Added #{licenses.join(', ')} to the permitted licenses"
end

auditable
desc 'remove LICENSE...', 'Remove one or more licenses from the permitted licenses'
def remove(*licenses)
assert_some licenses
modifying { licenses.each { |l| decisions.unpermit(l, txn) } }
say "Removed #{licenses.join(', ')} from the license permitted licenses"
printer.say "Removed #{licenses.join(', ')} from the license permitted licenses"
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/license_finder/cli/project_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ class ProjectName < Base

desc 'show', 'Show the project name'
def show
say 'Project Name:', :blue
say decisions.project_name
printer.say 'Project Name:', :blue
printer.say decisions.project_name
end

auditable
desc 'add NAME', 'Set the project name'
def add(name)
modifying { decisions.name_project(name, txn) }

say "Set the project name to #{name}", :green
printer.say "Set the project name to #{name}", :green
end

auditable
desc 'remove', 'Remove the project name'
def remove
modifying { decisions.unname_project(txn) }

say 'Removed the project name'
printer.say 'Removed the project name'
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/license_finder/cli/restricted_licenses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RestrictedLicenses < Base

desc 'list', 'List all the restricted licenses'
def list
say 'Restricted Licenses:', :blue
printer.say 'Restricted Licenses:', :blue
say_each(decisions.restricted, &:name)
end

Expand All @@ -17,15 +17,15 @@ def list
def add(*licenses)
assert_some licenses
modifying { licenses.each { |l| decisions.restrict(l, txn) } }
say "Added #{licenses.join(', ')} to the restricted licenses"
printer.say "Added #{licenses.join(', ')} to the restricted licenses"
end

auditable
desc 'remove LICENSE...', 'Remove one or more licenses from the restricted licenses'
def remove(*licenses)
assert_some licenses
modifying { licenses.each { |l| decisions.unrestrict(l, txn) } }
say "Removed #{licenses.join(', ')} from the restricted licenses"
printer.say "Removed #{licenses.join(', ')} from the restricted licenses"
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/license_finder/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def self.default_logger
Logger.new
end

# def self.default_printer
# Printer.new
# end

# Default +options+:
# {
# project_path: Pathname.pwd
Expand All @@ -31,6 +35,7 @@ def self.default_logger
# rebar_deps_dir: "deps",
# }
def initialize(configuration)
@printer = Printer.new
@logger = Logger.new(configuration.logger_mode)
@config = configuration
@scanner = Scanner.new(options)
Expand Down Expand Up @@ -68,7 +73,7 @@ def prepare_projects

private

attr_reader :logger
attr_reader :logger, :printer

# The core of the system. The saved decisions are applied to the current
# packages.
Expand Down
Loading

0 comments on commit b118772

Please sign in to comment.