Class: Chelsea::TextFormatter

Inherits:
Formatter show all
Defined in:
lib/chelsea/formatters/text.rb

Overview

Produce output in text format

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TextFormatter

Returns a new instance of TextFormatter.



27
28
29
30
31
# File 'lib/chelsea/formatters/text.rb', line 27

def initialize(options)
  super()
  @options = options
  @pastel = Pastel.new
end

Instance Method Details

#do_print(results) ⇒ Object

rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize



76
77
78
# File 'lib/chelsea/formatters/text.rb', line 76

def do_print(results)
  puts results
end

#fetch_results(server_response, reverse_dependencies) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chelsea/formatters/text.rb', line 34

def fetch_results(server_response, reverse_dependencies) # rubocop:disable Metrics/MethodLength
  response = ''
  if @options[:verbose]
    response += "\n"\
    "Audit Results\n"\
    "=============\n"
  end

  vuln_count = server_response.count do |vuln|
    vuln['vulnerabilities'].length.positive?
  end
  server_response.sort! { |x| x['vulnerabilities'].count }
  server_response.each.with_index do |r, idx|
    name, version = r['coordinates'].sub('pkg:gem/', '').split('@')
    reverse_deps = reverse_dependencies["#{name}-#{version}"]
    if r['vulnerabilities'].length.positive?
      response += @pastel.red(
        "[#{idx}/#{server_response.count}] - #{r['coordinates']} "
      )
      response += @pastel.red.bold("Vulnerable.\n")
      response += _get_reverse_deps(reverse_deps, name) if reverse_deps
      r['vulnerabilities'].each do |k, _|
        response += _format_vuln(k)
      end
    elsif @options[:verbose]
      response += @pastel.white(
        "[#{idx}/#{server_response.count}] - #{r['coordinates']} "
      )
      response += @pastel.green.bold("No vulnerabilities found!\n")
      response += _get_reverse_deps(reverse_deps, name) if reverse_deps
    end
  end

  table = TTY::Table.new(
    ['Dependencies Audited', 'Vulnerable Dependencies'],
    [[server_response.count, vuln_count]]
  )
  response += table.render(:unicode)
  response
end