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)


100
101
102
# File 'lib/bundler/sbom/cli.rb', line 100

def self.exit_on_failure?
  true
end

Instance Method Details

#dumpObject



11
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
44
45
# File 'lib/bundler/sbom/cli.rb', line 11

def dump
  format = options[:format].downcase
  sbom_format = options[:sbom].downcase

  # Validate output format
  unless ["json", "xml"].include?(format)
    Bundler.ui.error("Error: Unsupported output format '#{format}'. Supported formats: json, xml")
    exit 1
  end

  # Validate SBOM format
  unless ["spdx", "cyclonedx"].include?(sbom_format)
    Bundler.ui.error("Error: Unsupported SBOM format '#{sbom_format}'. Supported formats: spdx, cyclonedx")
    exit 1
  end

  # Generate SBOM based on specified format
  sbom = Bundler::Sbom::Generator.generate_sbom(sbom_format)

  # Determine file extension based on output format
  ext = format == "json" ? "json" : "xml"

  # Determine filename prefix based on SBOM format
  prefix = sbom_format == "spdx" ? "bom" : "bom-cyclonedx"
  output_file = "#{prefix}.#{ext}"

  if format == "json"
    File.write(output_file, JSON.pretty_generate(sbom))
  else # xml
    xml_content = Bundler::Sbom::Generator.convert_to_xml(sbom)
    File.write(output_file, xml_content)
  end

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

#licenseObject



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
90
91
92
93
94
95
96
97
# File 'lib/bundler/sbom/cli.rb', line 50

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

  # Validate format if provided
  if format && !["json", "xml"].include?(format)
    Bundler.ui.error("Error: Unsupported format '#{format}'. Supported formats: json, xml")
    exit 1
  end

  # Determine input file based on format or find default files
  if input_file.nil?
    if format == "xml" || (format.nil? && File.exist?("bom.xml"))
      input_file = "bom.xml"
    elsif File.exist?("bom-cyclonedx.json")
      input_file = "bom-cyclonedx.json"
    elsif File.exist?("bom-cyclonedx.xml")
      input_file = "bom-cyclonedx.xml"
    else
      input_file = "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"
    Bundler.ui.error("Error: #{input_file} not found. Run 'bundle sbom dump --format=#{file_type} --sbom=#{sbom_type}' first.")
    exit 1
  end

  begin
    content = File.read(input_file)

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

    Bundler::Sbom::Reporter.display_license_report(sbom)
  rescue JSON::ParserError
    Bundler.ui.error("Error: #{input_file} is not a valid JSON file")
    exit 1
  rescue StandardError => e
    Bundler.ui.error("Error processing #{input_file}: #{e.message}")
    exit 1
  end
end