Skip to content

Commit

Permalink
Merge 80d7189 into 2b54b0b
Browse files Browse the repository at this point in the history
  • Loading branch information
miah committed May 15, 2019
2 parents 2b54b0b + 80d7189 commit 17a7234
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 95 deletions.
50 changes: 23 additions & 27 deletions lib/inspec/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,32 @@ def check(path) # rubocop:disable Metrics/AbcSize
puts JSON.generate(result)
else
%w{location profile controls timestamp valid}.each do |item|
puts format('%-12s %s', item.to_s.capitalize + ':',
mark_text(result[:summary][item.to_sym]))
prepared_string = format('%-12s %s',
"#{item.to_s.capitalize} :",
result[:summary][item.to_sym])
ui.plain_line(prepared_string)
end
puts

if result[:errors].empty? and result[:warnings].empty?
puts 'No errors or warnings'
ui.plain_line('No errors or warnings')
else
red = "\033[31m"
yellow = "\033[33m"
rst = "\033[0m"

item_msg = lambda { |item|
pos = [item[:file], item[:line], item[:column]].compact.join(':')
pos.empty? ? item[:msg] : pos + ': ' + item[:msg]
}
result[:errors].each do |item|
puts "#{red}#{item_msg.call(item)}#{rst}"
end
result[:warnings].each do |item|
puts "#{yellow} ! #{item_msg.call(item)}#{rst}"
end

result[:errors].each { |item| ui.red " #{Inspec::UI::GLYPHS[:script_x]} #{item_msg.call(item)}\n" }
result[:warnings].each { |item| ui.yellow " ! #{item_msg.call(item)}\n" }

puts
puts format('Summary: %s%d errors%s, %s%d warnings%s',
red, result[:errors].length, rst,
yellow, result[:warnings].length, rst)

errors = ui.red("#{result[:errors].length} errors", print: false)
warnings = ui.yellow("#{result[:warnings].length} warnings", print: false)
ui.plain_line("Summary: #{errors}, #{warnings}")
end
end
exit 1 unless result[:summary][:valid]
ui.exit Inspec::UI::EXIT_USAGE_ERROR unless result[:summary][:valid]
rescue StandardError => e
pretty_handle_exception(e)
end
Expand Down Expand Up @@ -178,11 +174,11 @@ def archive(path)

if result && !o[:ignore_errors] == false
o[:logger].info 'Profile check failed. Please fix the profile before generating an archive.'
return exit 1
return ui.exit Inspec::UI::EXIT_USAGE_ERROR
end

# generate archive
exit 1 unless profile.archive(o)
ui.exit Inspec::UI::EXIT_USAGE_ERROR unless profile.archive(o)
rescue StandardError => e
pretty_handle_exception(e)
end
Expand Down Expand Up @@ -269,10 +265,10 @@ def exec(*targets)
runner = Inspec::Runner.new(o)
targets.each { |target| runner.add_target(target) }

exit runner.run
ui.exit runner.run
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message
exit 1
ui.exit Inspec::UI::EXIT_USAGE_ERROR
rescue StandardError => e
pretty_handle_exception(e)
end
Expand All @@ -287,12 +283,12 @@ def detect
if o['format'] == 'json'
puts res.to_json
else
headline('Platform Details')
puts Inspec::BaseCLI.format_platform_info(params: res, indent: 0, color: 36)
ui.headline('Platform Details')
ui.plain Inspec::BaseCLI.format_platform_info(params: res, indent: 0, color: 36)
end
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message
exit 1
ui.exit Inspec::UI::EXIT_USAGE_ERROR
rescue StandardError => e
pretty_handle_exception(e)
end
Expand Down Expand Up @@ -323,12 +319,12 @@ def shell_func
end

run_type, res = run_command(o)
exit res unless run_type == :ruby_eval
ui.exit res unless run_type == :ruby_eval

# No InSpec tests - just print evaluation output.
res = (res.respond_to?(:to_json) ? res.to_json : JSON.dump(res)) if o['reporter']&.keys&.include?('json')
puts res
exit 0
ui.exit Inspec::UI::EXIT_NORMAL
rescue RuntimeError, Train::UserError => e
$stderr.puts e.message
rescue StandardError => e
Expand Down Expand Up @@ -453,5 +449,5 @@ def run_command(opts)
else
Inspec::Log.error 'Run again with --debug for a stacktrace.'
end
exit 2
ui.exit Inspec::UI::EXIT_PLUGIN_ERROR
end

0 comments on commit 17a7234

Please sign in to comment.