Class: Brew::Vulns::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/brew/vulns/cli.rb

Constant Summary collapse

DEFAULT_MAX_SUMMARY =
60
SEVERITY_LEVELS =
{ "low" => 1, "medium" => 2, "high" => 3, "critical" => 4 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brew/vulns/cli.rb', line 13

def initialize(args)
  @args = args
  @formula_filter = args.first unless args.first&.start_with?("-")
  @include_deps = args.include?("--deps") || args.include?("-d")
  @json_output = args.include?("--json") || args.include?("-j")
  @sarif_output = args.include?("--sarif")
  @cyclonedx_output = args.include?("--cyclonedx")
  @help = args.include?("--help") || args.include?("-h")
  @max_summary = parse_max_summary(args)
  @min_severity = parse_severity(args)
  @brewfile = parse_brewfile_path(args)
end

Class Method Details

.run(args) ⇒ Object



6
7
8
# File 'lib/brew/vulns/cli.rb', line 6

def self.run(args)
  new(args).run
end

Instance Method Details

#parse_brewfile_path(args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/brew/vulns/cli.rb', line 51

def parse_brewfile_path(args)
  args.each_with_index do |arg, idx|
    if arg == "--brewfile" || arg == "-b"
      value = args[idx + 1]
      return value if value && !value.start_with?("-")
      return "Brewfile"
    elsif arg.start_with?("--brewfile=")
      return arg.split("=", 2).last
    end
  end
  nil
end

#parse_max_summary(args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brew/vulns/cli.rb', line 26

def parse_max_summary(args)
  args.each_with_index do |arg, idx|
    if arg == "--max-summary" || arg == "-m"
      value = args[idx + 1]
      return value.to_i if value && !value.start_with?("-")
    elsif arg.start_with?("--max-summary=")
      return arg.split("=", 2).last.to_i
    end
  end
  DEFAULT_MAX_SUMMARY
end

#parse_severity(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brew/vulns/cli.rb', line 38

def parse_severity(args)
  args.each_with_index do |arg, idx|
    if arg == "--severity" || arg == "-s"
      value = args[idx + 1]
      return SEVERITY_LEVELS[value&.downcase] || 0 if value && !value.start_with?("-")
    elsif arg.start_with?("--severity=")
      value = arg.split("=", 2).last
      return SEVERITY_LEVELS[value&.downcase] || 0
    end
  end
  0
end

#runObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/brew/vulns/cli.rb', line 64

def run
  if @help
    print_help
    return 0
  end

  formulae = load_formulae
  if formulae.empty?
    puts "No installed formulae found."
    return 0
  end

  queryable = formulae.select(&:supported_forge?).select(&:tag)
  skipped = formulae.size - queryable.size

  unless @json_output || @sarif_output || @cyclonedx_output
    puts "Checking #{queryable.size} packages for vulnerabilities..."
    puts "(#{skipped} packages skipped - no supported source URL)" if skipped > 0
    puts
  end

  results = scan_vulnerabilities(queryable)
  output_results(results, formulae)
rescue OsvClient::Error => e
  $stderr.puts "Error querying OSV: #{e.message}"
  1
rescue Error => e
  $stderr.puts "Error: #{e.message}"
  1
rescue JSON::ParserError => e
  $stderr.puts "Error parsing brew output: #{e.message}"
  1
end