class LicenseScout::CLI

Public Instance Methods

run(argv = ARGV) click to toggle source
# File lib/license_scout/cli.rb, line 78
def run(argv = ARGV)
  parse_options(argv)

  LicenseScout::Config.merge!(config)

  Mixlib::Log::Formatter.show_time = false
  LicenseScout::Log.level = LicenseScout::Config.log_level

  LicenseScout::Config.config_files.each do |config_file|
    if config_file =~ /^http/
      require "open-uri" unless defined?(OpenURI)

      LicenseScout::Log.info("[cli] Loading config from #{config_file}")

      Dir.mktmpdir do |dir|
        local_tmp_file = File.join(dir, File.basename(config_file))
        IO.copy_stream(open(config_file), local_tmp_file)
        LicenseScout::Config.from_file(local_tmp_file)
      end
    else
      full_config_file = File.expand_path(config_file)

      if File.exist?(full_config_file)
        LicenseScout::Log.info("[cli] Loading config from #{full_config_file}")
        LicenseScout::Config.from_file(full_config_file)
      else
        LicenseScout::Log.warn("[cli] Could not find #{full_config_file} -- skipping")
      end
    end
  end

  LicenseScout::Config.validate!

  case cli_arguments[0]
  when "export"
    json_file = cli_arguments[1]
    export_format = config[:format]

    exporter = LicenseScout::Exporter.new(json_file, export_format)
    exporter.export
  else
    collector = LicenseScout::Collector.new
    collector.collect

    reporter = LicenseScout::Reporter.new(collector.dependencies)
    reporter.report
  end
  exit 0
rescue Exceptions::FailExit
  exit 1
end