Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Plugin-System for CLI Plugins #421

Merged
merged 3 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions bin/inspec
Original file line number Diff line number Diff line change
Expand Up @@ -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)'

Expand Down Expand Up @@ -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],
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!! :D

}

# start the CLI
Inspec::InspecCLI.start(ARGV)
3 changes: 3 additions & 0 deletions lib/bundles/README.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 5 additions & 3 deletions lib/inspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
$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 'matchers/matchers'
require 'inspec/runner'
# ensure resource and plugins are loaded after runner, because the runner loads
# targets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch!

require 'inspec/resource'
require 'inspec/plugins'
require 'inspec/shell'
require 'matchers/matchers'
7 changes: 7 additions & 0 deletions lib/inspec/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
require 'forwardable'

module Inspec
# Resource Plugins
module Plugins
autoload :Resource, 'inspec/plugins/resource'
autoload :CLI, 'inspec/plugins/cli'
end

class PluginCtl
Expand All @@ -21,6 +23,11 @@ def initialize(home = nil)
.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]
Expand Down
24 changes: 24 additions & 0 deletions lib/inspec/plugins/cli.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions test/unit/plugin_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter

require 'helper'
require 'inspec/plugins'
require 'thor'

describe 'plugin system' do

describe 'with an empty profile' do
let(:cli_reg) { Inspec::Plugins::CLI }

it 'is empty' do
cli_reg.registry.must_equal({})
end

it 'stores one cli plugin' do
plugin = {
klass: Thor.new,
subcommand_name: 'my_cmd',
usage: 'usage my_cmd',
description: 'desc of my_cmd',
options: { test: 1 }
}
cli_reg.register(
plugin[:klass],
plugin[:subcommand_name],
plugin[:usage],
plugin[:description],
plugin[:options]
)
cli_reg.registry['my_cmd'].must_equal(plugin)
end
end
end