class Bundler::Audit::CLI

def check

def check
  scanner    = Scanner.new
  vulnerable = false
  scanner.scan(:ignore => options.ignore) do |result|
    vulnerable = true
    case result
    when Scanner::InsecureSource
      print_warning "Insecure Source URI found: #{result.source}"
    when Scanner::UnpatchedGem
      print_advisory result.gem, result.advisory
    end
  end
  if vulnerable
    say "Unpatched versions found!", :red
    exit 1
  else
    say "No unpatched versions found", :green
  end
end

def print_advisory(gem, advisory)

def print_advisory(gem, advisory)
  say "Name: ", :red
  say gem.name
  say "Version: ", :red
  say gem.version
  say "Advisory: ", :red
  say advisory.id
  say "Criticality: ", :red
  case advisory.criticality
  when :low    then say "Low"
  when :medium then say "Medium", :yellow
  when :high   then say "High", [:red, :bold]
  else              say "Unknown"
  end
  say "URL: ", :red
  say advisory.url
  if options.verbose?
    say "Description:", :red
    say
    print_wrapped advisory.description, :indent => 2
    say
  else
    say "Title: ", :red
    say advisory.title
  end
  unless advisory.patched_versions.empty?
    say "Solution: upgrade to ", :red
    say advisory.patched_versions.join(', ')
  else
    say "Solution: ", :red
    say "remove or disable this gem until a patch is available!", [:red, :bold]
  end
  say
end

def print_warning(message)

def print_warning(message)
  say message, :yellow
end

def say(message="", color=nil)

def say(message="", color=nil)
  color = nil unless $stdout.tty?
  super(message.to_s, color)
end

def update

def update
  say "Updating ruby-advisory-db ..."
  Database.update!
  puts "ruby-advisory-db: #{Database.new.size} advisories"
end

def version

def version
  database = Database.new
  puts "#{File.basename($0)} #{VERSION} (advisories: #{database.size})"
end