Skip to content

Commit daf8e00

Browse files
add support for format option and json bom output
Signed-off-by: Jeffrey Zhang <[email protected]>
1 parent c96d229 commit daf8e00

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ cyclonedx-ruby [options]
2929

3030
`-v, --[no-]verbose` Run verbosely
3131
`-p, --path path` Path to Ruby project directory
32+
`-f, --format` Bom output format
3233
`-h, --help` Show help message
3334

34-
**Output:** bom.xml file in project directory
35+
**Output:** bom.xml or bom.json file in project directory
3536

3637
#### Example
3738
```bash

lib/bom_builder.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@
3131
require 'rest_client'
3232
require 'securerandom'
3333
require_relative 'bom_helpers'
34+
require 'active_support/core_ext/hash'
3435

3536
class Bombuilder
3637
def self.build(path)
3738
original_working_directory = Dir.pwd
3839
setup(path)
3940
specs_list
40-
bom = build_bom(@gems)
41+
bom = build_bom(@gems, @bom_output_format)
4142

4243
begin
4344
@logger.info("Changing directory to the original working directory located at #{original_working_directory}")
@@ -84,6 +85,9 @@ def self.setup(path)
8485
opts.on('-o', '--output bom_file_path', '(Optional) Path to output the bom.xml file to') do |bom_file_path|
8586
@options[:bom_file_path] = bom_file_path
8687
end
88+
opts.on('-f', '--format bom_output_format', '(Optional) Output format for bom. Currently support xml (default) and json.') do |bom_output_format|
89+
@options[:bom_output_format] = bom_output_format
90+
end
8791
opts.on_tail('-h', '--help', 'Show help message') do
8892
puts opts
8993
exit
@@ -119,8 +123,17 @@ def self.setup(path)
119123
abort
120124
end
121125

126+
if @options[:bom_output_format].nil? || @options[:bom_output_format] == "xml"
127+
@bom_output_format = 'xml'
128+
elsif @options[:bom_output_format] == "json"
129+
@bom_output_format = 'json'
130+
else
131+
@logger.error("Unrecognized cyclonedx bom output format provided: #{@options[:bom_output_format]}")
132+
abort
133+
end
134+
122135
@bom_file_path = if @options[:bom_file_path].nil?
123-
'./bom.xml'
136+
"./bom.#{@bom_output_format}"
124137
else
125138
@options[:bom_file_path]
126139
end

lib/bom_helpers.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def random_urn_uuid
2929
"urn:uuid:#{SecureRandom.uuid}"
3030
end
3131

32-
def build_bom(gems)
32+
def build_bom(gems, format)
3333
builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
3434
attributes = { 'xmlns' => 'http://cyclonedx.org/schema/bom/1.1', 'version' => '1', 'serialNumber' => random_urn_uuid }
3535
xml.bom(attributes) do
@@ -61,7 +61,16 @@ def build_bom(gems)
6161
end
6262
end
6363
end
64-
builder.to_xml
64+
65+
xml = builder.to_xml
66+
67+
# Format verified to be either xml (default) or json in setup
68+
if format == 'json'
69+
JSON.pretty_generate(Hash.from_xml(xml))
70+
else
71+
xml
72+
end
73+
6574
end
6675

6776
def get_gem(name, version)

0 commit comments

Comments
 (0)