diff --git a/bin/inspec b/bin/inspec index 1a7757c47..eddf9d729 100755 --- a/bin/inspec +++ b/bin/inspec @@ -10,7 +10,7 @@ require 'pp' require_relative '../lib/inspec' require_relative '../lib/utils/json_log' -class InspecCLI < Thor # rubocop:disable Metrics/ClassLength +class Inspec::InspecCLI < Thor # rubocop:disable Metrics/ClassLength class_option :diagnose, type: :boolean, desc: 'Show diagnostics (versions, configurations)' @@ -251,4 +251,17 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength print "\n" end end -InspecCLI.start(ARGV) + +# load CLI plugins before the Inspec CLI has been started +Inspec::Plugins::CLI.registry.each{ |subcommand, params| + Inspec::InspecCLI.register( + params[:klass], + params[:subcommand_name], + params[:usage], + params[:description], + params[:options] + ) +} + +# start the CLI +Inspec::InspecCLI.start(ARGV) diff --git a/lib/bundles/README.md b/lib/bundles/README.md new file mode 100644 index 000000000..7925ba68b --- /dev/null +++ b/lib/bundles/README.md @@ -0,0 +1,3 @@ +# InSpec Bundled Plugins + +This directory contains bundled InSpec plugins. Those plugins are shipped with InSpec temporarily only. Over the next months we are going to stabilize the InSpec Plugin API. Once this API reached stability, all bundled plugins will be externalized. diff --git a/lib/inspec.rb b/lib/inspec.rb index c22061609..1e427e302 100644 --- a/lib/inspec.rb +++ b/lib/inspec.rb @@ -11,11 +11,13 @@ libdir = File.dirname(__FILE__) $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) require 'inspec/version' -require 'inspec/plugins' require 'inspec/profile' -require 'inspec/resource' require 'inspec/rspec_json_formatter' require 'inspec/rule' -require 'inspec/runner' -require 'inspec/shell' require 'matchers/matchers' +require 'inspec/runner' +# ensure resource and plugins are loaded after runner, because the runner loads +# targets +require 'inspec/resource' +require 'inspec/plugins' +require 'inspec/shell' diff --git a/lib/inspec/plugins.rb b/lib/inspec/plugins.rb index 526d26a44..de15c9952 100644 --- a/lib/inspec/plugins.rb +++ b/lib/inspec/plugins.rb @@ -3,10 +3,12 @@ # author: Christoph Hartmann require 'forwardable' - +puts 'plugins' module Inspec + # Resource Plugins module Plugins autoload :Resource, 'inspec/plugins/resource' + autoload :CLI, 'inspec/plugins/cli' end class PluginCtl @@ -21,6 +23,11 @@ module Inspec .map { |x| File.dirname(x) } .map { |x| Dir[File.join(x, 'lib', 'inspec-*.rb')] } .flatten + + # load bundled plugins + bundled_dir = File.expand_path(File.dirname(__FILE__)) + @paths += Dir[File.join(bundled_dir, '..', 'bundles', 'inspec-*.rb')].flatten + # map paths to names @registry = Hash[@paths.map { |x| [File.basename(x, '.rb'), x] diff --git a/lib/inspec/plugins/cli.rb b/lib/inspec/plugins/cli.rb new file mode 100644 index 000000000..80baf9d25 --- /dev/null +++ b/lib/inspec/plugins/cli.rb @@ -0,0 +1,24 @@ +# encoding: utf-8 +# author: Christoph Hartmann +# author: Dominik Richter + +module Inspec + module Plugins + # stores all CLI plugin, we expect those to the `Thor` subclasses + class CLI + def self.registry + @registry ||= {} + end + + def self.register(klass, subcommand_name, usage, description, options = {}) + registry[subcommand_name] = { + klass: klass, + subcommand_name: subcommand_name, + usage: usage, + description: description, + options: options, + } + end + end + end +end