add registry for cli plugins

This commit is contained in:
Christoph Hartmann 2016-02-05 12:22:23 +01:00
parent 5e1c9e24fa
commit 589db0bcd0
5 changed files with 56 additions and 7 deletions

View file

@ -10,7 +10,7 @@ require 'pp'
require_relative '../lib/inspec' require_relative '../lib/inspec'
require_relative '../lib/utils/json_log' 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, class_option :diagnose, type: :boolean,
desc: 'Show diagnostics (versions, configurations)' desc: 'Show diagnostics (versions, configurations)'
@ -251,4 +251,17 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength
print "\n" print "\n"
end end
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)

3
lib/bundles/README.md Normal file
View file

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

View file

@ -11,11 +11,13 @@ libdir = File.dirname(__FILE__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require 'inspec/version' require 'inspec/version'
require 'inspec/plugins'
require 'inspec/profile' require 'inspec/profile'
require 'inspec/resource'
require 'inspec/rspec_json_formatter' require 'inspec/rspec_json_formatter'
require 'inspec/rule' require 'inspec/rule'
require 'inspec/runner'
require 'inspec/shell'
require 'matchers/matchers' 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'

View file

@ -3,10 +3,12 @@
# author: Christoph Hartmann # author: Christoph Hartmann
require 'forwardable' require 'forwardable'
puts 'plugins'
module Inspec module Inspec
# Resource Plugins
module Plugins module Plugins
autoload :Resource, 'inspec/plugins/resource' autoload :Resource, 'inspec/plugins/resource'
autoload :CLI, 'inspec/plugins/cli'
end end
class PluginCtl class PluginCtl
@ -21,6 +23,11 @@ module Inspec
.map { |x| File.dirname(x) } .map { |x| File.dirname(x) }
.map { |x| Dir[File.join(x, 'lib', 'inspec-*.rb')] } .map { |x| Dir[File.join(x, 'lib', 'inspec-*.rb')] }
.flatten .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 # map paths to names
@registry = Hash[@paths.map { |x| @registry = Hash[@paths.map { |x|
[File.basename(x, '.rb'), x] [File.basename(x, '.rb'), x]

24
lib/inspec/plugins/cli.rb Normal file
View file

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