Skip to content

Commit

Permalink
Merge pull request #213 from stelligent/colorize-text-output
Browse files Browse the repository at this point in the history
#146 Colorize text output format based on violation type
  • Loading branch information
Jesse Adams authored May 30, 2019
2 parents 0541272 + 0acda2d commit 0003ffc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions cfn-nag.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
# they are coupled and we are doing a good bit of experimenting in cfn-model
# i might consider collapsing them again....
s.add_runtime_dependency('cfn-model', '0.4.0')
s.add_runtime_dependency('colorize', '0.8.1')
s.add_runtime_dependency('jmespath', '~> 1.3.1')
s.add_runtime_dependency('logging', '~> 2.2.2')
s.add_runtime_dependency('netaddr', '~> 1.5.1')
Expand Down
19 changes: 13 additions & 6 deletions lib/cfn-nag/result_view/simple_stdout_results.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

require 'cfn-nag/violation'
require 'colorize'

# Print results to STDOUT
class SimpleStdoutResults
def message_violations(violations)
violations.each do |violation|
color = violation.type == 'FAIL' ? :red : :yellow

message message_type: "#{violation.type} #{violation.id}",
color: color,
message: violation.message,
logical_resource_ids: violation.logical_resource_ids,
line_numbers: violation.line_numbers
Expand Down Expand Up @@ -37,7 +41,9 @@ def render(results)

private

# rubocop:disable Metrics/AbcSize
def message(message_type:,
color:,
message:,
logical_resource_ids: nil,
line_numbers: [])
Expand All @@ -46,13 +52,14 @@ def message(message_type:,

60.times { print '-' }
puts
puts "| #{message_type.upcase}"
puts '|'
puts "| Resources: #{logical_resource_ids}" unless logical_resource_ids.nil?
puts "| Line Numbers: #{line_numbers}" unless line_numbers.empty?
puts '|' unless line_numbers.empty? && logical_resource_ids.nil?
puts "| #{message}"
puts "| #{message_type.upcase}".send(color)
puts '|'.send(color)
puts "| Resources: #{logical_resource_ids}".send(color) unless logical_resource_ids.nil?
puts "| Line Numbers: #{line_numbers}".send(color) unless line_numbers.empty?
puts '|'.send(color) unless line_numbers.empty? && logical_resource_ids.nil?
puts "| #{message}".send(color)
end
# rubocop:enable Metrics/AbcSize

def indent_multiline_string_with_prefix(prefix, multiline_string)
prefix + ' ' + multiline_string.gsub(/\n/, "\n#{prefix} ")
Expand Down
48 changes: 33 additions & 15 deletions spec/cfn_nag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,43 @@
describe '#audit_aggregate_across_files_and_render_results' do
context 'json output' do
it 'renders json results' do
@cfn_nag.audit_aggregate_across_files_and_render_results(
input_path: test_template_path('json/s3_bucket_policy/s3_bucket_with_wildcards.json'),
output_format: 'json'
)
# measuring stdout assertion here is going to be a pain and maybe
# fragile as rules are added just make sure the rendering doesn't
# blow i guess
expect {
@cfn_nag.audit_aggregate_across_files_and_render_results(
input_path: test_template_path('json/s3_bucket_policy/s3_bucket_with_wildcards.json'),
output_format: 'json'
)
}.to output(/"type": "FAIL"/).to_stdout
end
end

context 'txt output' do
it 'renders json results' do
@cfn_nag.audit_aggregate_across_files_and_render_results(
input_path: test_template_path('json/s3_bucket_policy/s3_bucket_with_wildcards.json'),
output_format: 'txt'
)
# measuring stdout assertion here is going to be a pain and maybe
# fragile as rules are added just make sure the rendering doesn't
# blow i guess
it 'renders text results' do
expect {
@cfn_nag.audit_aggregate_across_files_and_render_results(
input_path: test_template_path('json/s3_bucket_policy/s3_bucket_with_wildcards.json'),
output_format: 'txt'
)
}.to output(/\| FAIL F15/).to_stdout
end

# \e[0;31;49m is the ANSI escape sequence for red
it 'colorizes failures as red' do
expect {
@cfn_nag.audit_aggregate_across_files_and_render_results(
input_path: test_template_path('json/s3_bucket_policy/s3_bucket_with_wildcards.json'),
output_format: 'txt'
)
}.to output(/\[0;31;49m/).to_stdout
end

# \e[0;33;49m is the ANSI escape sequence for yellow
it 'colorizes warnings as yellow' do
expect {
@cfn_nag.audit_aggregate_across_files_and_render_results(
input_path: test_template_path('yaml/security_group/sg_with_suppression.yml'),
output_format: 'txt'
)
}.to output(/\[0;33;49m/).to_stdout
end
end
end
Expand Down

0 comments on commit 0003ffc

Please sign in to comment.