lib/spandx/cli/commands/scan.rb in spandx-0.7.0 vs lib/spandx/cli/commands/scan.rb in spandx-0.8.0
- old
+ new
@@ -2,26 +2,48 @@
module Spandx
module Cli
module Commands
class Scan < Spandx::Cli::Command
- attr_reader :lockfile
+ attr_reader :scan_path
- def initialize(lockfile, options)
- @lockfile = lockfile ? ::Pathname.new(File.expand_path(lockfile)) : nil
+ def initialize(scan_path, options)
+ @scan_path = ::Pathname.new(scan_path)
@options = options
end
def execute(output: $stdout)
- if lockfile.nil?
- output.puts 'OK'
- else
- report = ::Spandx::Core::Report.new
- ::Spandx::Core::Parser.for(lockfile).parse(lockfile).each do |dependency|
+ report = ::Spandx::Core::Report.new
+ each_file_in(scan_path) do |file|
+ each_dependency_from(file) do |dependency|
report.add(dependency)
end
- output.puts report.to_json
end
+ output.puts report.to_json
+ end
+
+ private
+
+ def recursive?
+ @options['recursive']
+ end
+
+ def each_file_in(dir, &block)
+ files = File.directory?(dir) ? Dir.glob(File.join(dir, '*')) : [dir]
+ files.each do |file|
+ if File.directory?(file)
+ each_file_in(file, &block) if recursive?
+ else
+ block.call(file)
+ end
+ end
+ end
+
+ def each_dependency_from(file)
+ ::Spandx::Core::Parser
+ .for(file)
+ .parse(file)
+ .each { |dependency| yield dependency }
end
end
end
end
end