Merge pull request #415 from chef/dr/plugin

establish plugin loading dock
This commit is contained in:
Christoph Hartmann 2016-02-04 15:07:55 +01:00
commit 8abbb675fe
2 changed files with 35 additions and 0 deletions

View file

@ -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'

View file

@ -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) }