class Inspec::InspecCLI
Public Instance Methods
archive(path)
click to toggle source
# File lib/inspec/cli.rb, line 173 def archive(path) o = config diagnose(o) o[:logger] = Logger.new($stdout) o[:logger].level = get_log_level(o[:log_level]) o[:backend] = Inspec::Backend.create(Inspec::Config.mock) # Force vendoring with overwrite when archiving vendor_options = o.dup vendor_options[:overwrite] = true vendor_deps(path, vendor_options) 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 ui.exit Inspec::UI::EXIT_USAGE_ERROR end # generate archive ui.exit Inspec::UI::EXIT_USAGE_ERROR unless profile.archive(o) rescue StandardError => e pretty_handle_exception(e) end
check(path)
click to toggle source
# File lib/inspec/cli.rb, line 98 def check(path) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength o = config diagnose(o) o["log_location"] ||= STDERR if o["format"] == "json" o["log_level"] ||= "warn" configure_logger(o) o[:backend] = Inspec::Backend.create(Inspec::Config.mock) o[:check_mode] = true o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache]) # run check profile = Inspec::Profile.for_target(path, o) result = profile.check if o["format"] == "json" puts JSON.generate(result) else %w{location profile controls timestamp valid}.each do |item| 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? ui.plain_line("No errors or warnings") else item_msg = lambda { |item| pos = [item[:file], item[:line], item[:column]].compact.join(":") pos.empty? ? item[:msg] : pos + ": " + item[:msg] } 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 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 ui.exit Inspec::UI::EXIT_USAGE_ERROR unless result[:summary][:valid] rescue StandardError => e pretty_handle_exception(e) end
clear_cache()
click to toggle source
# File lib/inspec/cli.rb, line 414 def clear_cache o = config configure_logger(o) cache_path = o[:vendor_cache] || "~/.inspec/cache" FileUtils.rm_r Dir.glob(File.expand_path(cache_path)) o[:logger] = Logger.new($stdout) o[:logger].level = get_log_level(o[:log_level]) o[:logger].info "== InSpec cache cleared successfully ==" end
detect()
click to toggle source
# File lib/inspec/cli.rb, line 298 def detect o = config o[:command] = "platform.params" configure_logger(o) (_, res) = run_command(o) if o["format"] == "json" puts res.to_json else ui.headline("Platform Details") ui.plain Inspec::BaseCLI.format_platform_info(params: res, indent: 0, color: 36, enable_color: ui.color?) end rescue ArgumentError, RuntimeError, Train::UserError => e $stderr.puts e.message ui.exit Inspec::UI::EXIT_USAGE_ERROR rescue StandardError => e pretty_handle_exception(e) end
env(shell = nil)
click to toggle source
# File lib/inspec/cli.rb, line 376 def env(shell = nil) p = Inspec::EnvPrinter.new(self.class, shell) p.print_and_exit! rescue StandardError => e pretty_handle_exception(e) end
exec(*targets)
click to toggle source
# File lib/inspec/cli.rb, line 279 def exec(*targets) o = config diagnose(o) configure_logger(o) runner = Inspec::Runner.new(o) targets.each { |target| runner.add_target(target) } ui.exit runner.run rescue ArgumentError, RuntimeError, Train::UserError => e $stderr.puts e.message ui.exit Inspec::UI::EXIT_USAGE_ERROR rescue StandardError => e pretty_handle_exception(e) end
json(target)
click to toggle source
# File lib/inspec/cli.rb, line 71 def json(target) require "json" unless defined?(JSON) o = config diagnose(o) o["log_location"] = $stderr configure_logger(o) o[:backend] = Inspec::Backend.create(Inspec::Config.mock) o[:check_mode] = true o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache]) profile = Inspec::Profile.for_target(target, o) dst = o[:output].to_s # Write JSON Inspec::Utils::JsonProfileSummary.produce_json( info: profile.info, write_path: dst ) rescue StandardError => e pretty_handle_exception(e) end
run_context()
click to toggle source
# File lib/inspec/cli.rb, line 394 def run_context require "inspec/utils/telemetry/run_context_probe" puts Inspec::Telemetry::RunContextProbe.guess_run_context end
schema(name)
click to toggle source
# File lib/inspec/cli.rb, line 384 def schema(name) require "inspec/schema/output_schema" puts Inspec::Schema::OutputSchema.json(name) rescue StandardError => e puts e puts "Valid schemas are #{Inspec::Schema::OutputSchema.names.join(", ")}" end
shell_func()
click to toggle source
# File lib/inspec/cli.rb, line 338 def shell_func o = config diagnose(o) o[:debug_shell] = true Inspec::Resource.toggle_inspect unless o[:inspect] log_device = suppress_log_output?(o) ? nil : $stdout o[:logger] = Logger.new(log_device) o[:logger].level = get_log_level(o[:log_level]) if o[:command].nil? runner = Inspec::Runner.new(o) return Inspec::Shell.new(runner).start end run_type, res = run_command(o) ui.exit res unless run_type == :ruby_eval # No InSpec tests - just print evaluation output. reporters = o["reporter"] || {} if reporters.keys.include?("json") res = if res.respond_to?(:to_json) res.to_json else JSON.dump(res) end end puts res ui.exit Inspec::UI::EXIT_NORMAL rescue RuntimeError, Train::UserError => e $stderr.puts e.message rescue StandardError => e pretty_handle_exception(e) end
vendor(path = nil)
click to toggle source
# File lib/inspec/cli.rb, line 150 def vendor(path = nil) o = config configure_logger(o) o[:logger] = Logger.new($stdout) o[:logger].level = get_log_level(o[:log_level]) vendor_deps(path, o) end
version()
click to toggle source
# File lib/inspec/cli.rb, line 401 def version if config["format"] == "json" v = { version: Inspec::VERSION } puts v.to_json else puts Inspec::VERSION end end
Private Instance Methods
run_command(opts)
click to toggle source
# File lib/inspec/cli.rb, line 427 def run_command(opts) runner = Inspec::Runner.new(Inspec::Config.new(opts)) res = runner.eval_with_virtual_profile(opts[:command]) runner.load return :ruby_eval, res if runner.all_rules.empty? return :rspec_run, runner.run_tests # rubocop:disable Style/RedundantReturn end