diff --git a/lib/inspec.rb b/lib/inspec.rb index 3c0cdde71..c22061609 100644 --- a/lib/inspec.rb +++ b/lib/inspec.rb @@ -11,6 +11,7 @@ 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' diff --git a/lib/inspec/plugins.rb b/lib/inspec/plugins.rb index be5c0b89e..526d26a44 100644 --- a/lib/inspec/plugins.rb +++ b/lib/inspec/plugins.rb @@ -2,8 +2,42 @@ # author: Dominik Richter # author: Christoph Hartmann +require 'forwardable' + module Inspec module Plugins autoload :Resource, 'inspec/plugins/resource' end + + class PluginCtl + extend Forwardable + + attr_reader :registry + def_delegator :registry, :keys, :list + + def initialize(home = nil) + @home = home || File.join(Dir.home, '.inspec', 'plugins') + @paths = Dir[File.join(@home, '**{,/*/**}', '*.gemspec')] + .map { |x| File.dirname(x) } + .map { |x| Dir[File.join(x, 'lib', 'inspec-*.rb')] } + .flatten + # map paths to names + @registry = Hash[@paths.map { |x| + [File.basename(x, '.rb'), x] + }] + end + + def load(name) + path = @registry[name] + if path.nil? + fail "Couldn't find plugin #{name}. Searching in #{@home}" + end + puts "Loading plugin #{name} from #{path}" + require path + end + end end + +# Load all plugins on startup +ctl = Inspec::PluginCtl.new +ctl.list.each { |x| ctl.load(x) }