提交 3d55b895 编写于 作者: N Nick

Support multiple output files with different formats

上级 cbf8c938
......@@ -27,8 +27,8 @@ module Brakeman
# * :ignore_model_output - consider models safe (default: false)
# * :message_limit - limit length of messages
# * :min_confidence - minimum confidence (0-2, 0 is highest)
# * :output_file - file for output
# * :output_format - format for output (:to_s, :to_tabs, :to_csv, :to_html)
# * :output_files - files for output
# * :output_formats - formats for output (:to_s, :to_tabs, :to_csv, :to_html)
# * :parallel_checks - run checks in parallel (default: true)
# * :print_report - if no output file specified, print to stdout (default: false)
# * :quiet - suppress most messages (default: true)
......@@ -65,7 +65,7 @@ module Brakeman
options = load_options(options[:config_file]).merge! options
options = get_defaults.merge! options
options[:output_format] = get_output_format options
options[:output_formats] = get_output_formats options
app_path = options[:app_path]
......@@ -124,39 +124,47 @@ module Brakeman
}
end
#Determine output format based on options[:output_format]
#or options[:output_file]
def self.get_output_format options
#Determine output formats based on options[:output_formats]
#or options[:output_files]
def self.get_output_formats options
#Set output format
if options[:output_format] && options[:output_files] && options[:output_files].size > 1
raise ArgumentError, "Cannot specify output format if multiple output files specified"
end
if options[:output_format]
case options[:output_format]
when :html, :to_html
:to_html
when :csv, :to_csv
:to_csv
when :pdf, :to_pdf
:to_pdf
when :tabs, :to_tabs
:to_tabs
when :json, :to_json
:to_json
else
:to_s
end
[
case options[:output_format]
when :html, :to_html
:to_html
when :csv, :to_csv
:to_csv
when :pdf, :to_pdf
:to_pdf
when :tabs, :to_tabs
:to_tabs
when :json, :to_json
:to_json
else
:to_s
end
]
else
case options[:output_file]
when /\.html$/i
:to_html
when /\.csv$/i
:to_csv
when /\.pdf$/i
:to_pdf
when /\.tabs$/i
:to_tabs
when /\.json$/i
:to_json
else
:to_s
return [:to_s] unless options[:output_files]
options[:output_files].map do |output_file|
case output_file
when /\.html$/i
:to_html
when /\.csv$/i
:to_csv
when /\.pdf$/i
:to_pdf
when /\.tabs$/i
:to_tabs
when /\.json$/i
:to_json
else
:to_s
end
end
end
end
......@@ -253,17 +261,21 @@ module Brakeman
end
tracker.run_checks
if options[:output_file]
if options[:output_files]
notify "Generating report..."
File.open options[:output_file], "w" do |f|
f.puts tracker.report.send(options[:output_format])
options[:output_files].each_with_index do |output_file, idx|
File.open output_file, "w" do |f|
f.write tracker.report.send(options[:output_formats][idx])
end
notify "Report saved in '#{output_file}'"
end
notify "Report saved in '#{options[:output_file]}'"
elsif options[:print_report]
notify "Generating report..."
puts tracker.report.send(options[:output_format])
options[:output_formats].each do |output_format|
puts tracker.report.send(output_format)
end
end
tracker
......
......@@ -130,7 +130,7 @@ module Brakeman::Options
opts.on "-f",
"--format TYPE",
[:pdf, :text, :html, :csv, :tabs, :json],
"Specify output format. Default is text" do |type|
"Specify output formats. Default is text." do |type|
type = "s" if type == :text
options[:output_format] = ("to_" << type.to_s).to_sym
......@@ -152,8 +152,9 @@ module Brakeman::Options
options[:message_limit] = limit.to_i
end
opts.on "-o", "--output FILE", "Specify file for output. Defaults to stdout" do |file|
options[:output_file] = file
opts.on "-o", "--output FILE", "Specify files for output. Defaults to stdout. Can specify multiple times for multiple formats." do |file|
options[:output_files] ||= []
options[:output_files].push(file)
end
opts.on "--separate-models", "Warn on each model without attr_accessible" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册