Class: Bundler::Sbom::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/bundler/sbom/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/bundler/sbom/cli.rb', line 91

def self.exit_on_failure?
  true
end

Instance Method Details

#dumpObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bundler/sbom/cli.rb', line 12

def dump
  format = options[:format].downcase
  sbom_format = options[:sbom].downcase
  without_groups = parse_without_groups(options[:without])

  unless ["json", "xml"].include?(format)
    raise Thor::Error, "Error: Unsupported output format '#{format}'. Supported formats: json, xml"
  end

  unless ["spdx", "cyclonedx"].include?(sbom_format)
    raise Thor::Error, "Error: Unsupported SBOM format '#{sbom_format}'. Supported formats: spdx, cyclonedx"
  end

  if sbom_format == "spdx" && format == "xml"
    raise Thor::Error, "Error: SPDX 2.3 does not define an XML serialization. Use '--format json' or '--sbom cyclonedx --format xml'."
  end

  generator = Bundler::Sbom::Generator.new(format: sbom_format, without_groups: without_groups)
  sbom = generator.generate

  ext = (format == "json") ? "json" : "xml"
  prefix = (sbom_format == "spdx") ? "bom" : "bom-cyclonedx"
  output_file = "#{prefix}.#{ext}"

  if format == "json"
    File.write(output_file, JSON.pretty_generate(sbom.to_hash))
  else
    File.write(output_file, sbom.to_xml)
  end

  Bundler.ui.info("Generated #{sbom_format.upcase} SBOM at #{output_file}")
end

#licenseObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bundler/sbom/cli.rb', line 48

def license
  format = options[:format]&.downcase
  input_file = options[:file]

  if format && !["json", "xml"].include?(format)
    raise Thor::Error, "Error: Unsupported format '#{format}'. Supported formats: json, xml"
  end

  if input_file.nil?
    input_file = if File.exist?("bom.json")
      "bom.json"
    elsif File.exist?("bom-cyclonedx.json")
      "bom-cyclonedx.json"
    elsif File.exist?("bom-cyclonedx.xml")
      "bom-cyclonedx.xml"
    else
      "bom.json"
    end
  end

  unless File.exist?(input_file)
    file_type = (File.extname(input_file) == ".xml") ? "xml" : "json"
    sbom_type = input_file.include?("cyclonedx") ? "cyclonedx" : "spdx"
    raise Thor::Error, "Error: #{input_file} not found. Run 'bundle sbom dump --format=#{file_type} --sbom=#{sbom_type}' first."
  end

  content = File.read(input_file)

  sbom = if format == "xml" || (!format && File.extname(input_file) == ".xml")
    Bundler::Sbom::Generator.parse_xml(content)
  else
    Bundler::Sbom::Generator.from_hash(JSON.parse(content))
  end

  Bundler::Sbom::Reporter.new(sbom).display_license_report
rescue JSON::ParserError
  raise Thor::Error, "Error: #{input_file} is not a valid JSON file"
rescue Thor::Error
  raise
rescue => e
  raise Thor::Error, "Error processing #{input_file}: #{e.message}"
end