lib/inspec/cli.rb in inspec-0.31.0 vs lib/inspec/cli.rb in inspec-0.32.0
- old
+ new
@@ -13,10 +13,16 @@
require 'inspec/plugins'
require 'inspec/runner_mock'
require 'inspec/env_printer'
class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
+ class_option :log_level, aliases: :l, type: :string,
+ desc: 'Set the log level: info (default), debug, warn, error'
+
+ class_option :log_location, type: :string,
+ desc: 'Location to send diagnostic log messages to. (default: STDOUT or STDERR)'
+
class_option :diagnose, type: :boolean,
desc: 'Show diagnostics (versions, configurations)'
desc 'json PATH', 'read all tests in PATH and generate a JSON summary'
option :output, aliases: :o, type: :string,
@@ -91,10 +97,18 @@
end
end
exit 1 unless result[:summary][:valid]
end
+ desc 'vendor', 'Download all dependencies and generate a lockfile'
+ def vendor(path = nil)
+ configure_logger(opts)
+ profile = Inspec::Profile.for_target('./', opts)
+ lockfile = profile.generate_lockfile(path)
+ File.write('inspec.lock', lockfile.to_yaml)
+ end
+
desc 'archive PATH', 'archive a profile to tar.gz (default) or zip'
profile_options
option :output, aliases: :o, type: :string,
desc: 'Save the archive to a path'
option :zip, type: :boolean, default: false,
@@ -126,10 +140,11 @@
desc 'exec PATHS', 'run all test files at the specified PATH.'
exec_options
def exec(*targets)
diagnose
+ configure_logger(opts)
o = opts.dup
# run tests
run_tests(targets, o)
end
@@ -138,11 +153,11 @@
target_options
option :format, type: :string
def detect
o = opts.dup
o[:command] = 'os.params'
- res = run_command(o)
+ (_, res) = run_command(o)
if opts['format'] == 'json'
puts res.to_json
else
headline('Operating System Details')
%w{name family release arch}.each { |item|
@@ -160,26 +175,27 @@
desc: 'Which formatter to use: cli, progress, documentation, json, json-min'
def shell_func
diagnose
o = opts.dup
- log_device = opts['format'] == 'json' ? nil : STDOUT
+ json_output = ['json', 'json-min'].include?(opts['format'])
+ log_device = json_output ? 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
- else
- res = run_command(o)
- if opts['format'] == 'json'
- jres = res.respond_to?(:to_json) ? res.to_json : JSON.dump(res)
- puts jres
- else
- puts res
- end
end
+
+ run_type, res = run_command(o)
+ 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 json_output
+ puts res
+ exit 0
rescue RuntimeError, Train::UserError => e
$stderr.puts e.message
end
desc 'env', 'Output shell-appropriate completion configuration'
@@ -194,12 +210,17 @@
end
private
def run_command(opts)
- opts[:test_collector] = Inspec::RunnerMock.new
runner = Inspec::Runner.new(opts)
- runner.create_context.load(opts[:command])
+ ctx = runner.create_context(opts)
+ res = ctx.load(opts[:command])
+
+ return :ruby_eval, res if ctx.rules.empty?
+
+ runner.register_rules(ctx)
+ return :rspec_run, runner.run # rubocop:disable Style/RedundantReturn
end
end
# Load all plugins on startup
ctl = Inspec::PluginCtl.new