lib/inspec/cli.rb in inspec-4.7.24 vs lib/inspec/cli.rb in inspec-4.10.4

- old
+ new

@@ -121,40 +121,36 @@ if o["format"] == "json" 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? && 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 desc "vendor PATH", "Download all dependencies and generate a lockfile in a `vendor` directory" @@ -201,15 +197,15 @@ profile = Inspec::Profile.for_target(path, o) result = profile.check 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 desc "exec LOCATIONS", "run all test files at the specified LOCATIONS." @@ -292,14 +288,14 @@ configure_logger(o) 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 desc "detect", "detect the target OS" @@ -310,16 +306,16 @@ o[:command] = "platform.params" (_, res) = run_command(o) 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 desc "shell", "open an interactive debugging shell" @@ -346,16 +342,16 @@ runner = Inspec::Runner.new(o) return Inspec::Shell.new(runner).start 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 pretty_handle_exception(e) end @@ -383,18 +379,11 @@ def version if config["format"] == "json" v = { version: Inspec::VERSION } puts v.to_json else - require "inspec/utils/latest_version" puts Inspec::VERSION - # display outdated version - # TODO: remove this. Don't notify of update to a gem when they install omnibus - latest = LatestInSpecVersion.new.latest || Inspec::VERSION - if Gem::Version.new(Inspec::VERSION) < Gem::Version.new(latest) - puts "\nYour version of #{Inspec::Dist::PRODUCT_NAME} is out of date! The latest version is #{latest}." - end end end map %w{-v --version} => :version desc "nothing", "does nothing" @@ -468,7 +457,7 @@ Inspec::Log.error v2ex.class.name Inspec::Log.error v2ex.backtrace.join("\n") else Inspec::Log.error "Run again with --debug for a stacktrace." end - exit 2 + ui.exit Inspec::UI::EXIT_PLUGIN_ERROR end