lib/inspec/cli.rb in inspec-3.9.3 vs lib/inspec/cli.rb in inspec-4.1.4.preview

- old
+ new

@@ -37,10 +37,13 @@ desc: 'Disable loading all plugins that are shipped in the lib/plugins directory of InSpec. Useful in development.' class_option :disable_user_plugins, type: :string, banner: '', desc: 'Disable loading all plugins that the user installed.' + require 'license_acceptance/cli_flags/thor' + include LicenseAcceptance::CLIFlags::Thor + desc 'json PATH', 'read all tests in PATH and generate a JSON summary' option :output, aliases: :o, type: :string, desc: 'Save the created profile to a path' option :controls, type: :array, desc: 'A list of controls to include. Ignore all other tests.' @@ -194,10 +197,11 @@ 1 Usage or general error 2 Error in plugin system 3 Fatal deprecation encountered 100 Normal exit, at least one test failed 101 Normal exit, at least one test skipped but none failed + 172 Chef License not accepted ``` Below are some examples of using `exec` with different test LOCATIONS: Automate: @@ -371,21 +375,47 @@ return :ruby_eval, res if runner.all_rules.empty? return :rspec_run, runner.run_tests # rubocop:disable Style/RedundantReturn end end +#=====================================================================# +# Pre-Flight Code +#=====================================================================# + +help_commands = ['-h', '--help', 'help'] +version_commands = ['-v', '--version', 'version'] +commands_exempt_from_license_check = help_commands + version_commands + +#---------------------------------------------------------------------# +# EULA acceptance +#---------------------------------------------------------------------# +require 'license_acceptance/acceptor' begin - # Handle help commands - # This allows you to use any of the normal help commands after the normal args. - help_commands = ['-h', '--help', 'help'] - (help_commands & ARGV).each do |cmd| - # move the help argument to one place behind the end for Thor to digest - if ARGV.size > 1 - match = ARGV.delete(cmd) - ARGV.insert(-2, match) - end + if (commands_exempt_from_license_check & ARGV.map(&:downcase)).empty? && # Did they use a non-exempt command? + !ARGV.empty? # Did they supply at least one command? + LicenseAcceptance::Acceptor.check_and_persist('inspec', Inspec::VERSION) end +rescue LicenseAcceptance::LicenseNotAcceptedError + Inspec::Log.error 'InSpec cannot execute without accepting the license' + Inspec::UI.new.exit(:license_not_accepted) +end +#---------------------------------------------------------------------# +# Adjustments for help handling +# This allows you to use any of the normal help commands after the normal args. +#---------------------------------------------------------------------# +(help_commands & ARGV).each do |cmd| + # move the help argument to one place behind the end for Thor to digest + if ARGV.size > 1 + match = ARGV.delete(cmd) + ARGV.insert(-2, match) + end +end + +#---------------------------------------------------------------------# +# Plugin Loading +#---------------------------------------------------------------------# +begin # Load v2 plugins. Manually check for plugin disablement. omit_core = ARGV.delete('--disable-core-plugins') omit_user = ARGV.delete('--disable-user-plugins') v2_loader = Inspec::Plugin::V2::Loader.new(omit_core_plugins: omit_core, omit_user_plugins: omit_user) v2_loader.load_all