core: allow command plugins to have options

This commit is contained in:
Mitchell Hashimoto 2014-01-11 08:38:27 -08:00
parent 6a7e07c53f
commit 4a99cdccdf
3 changed files with 34 additions and 16 deletions

View File

@ -11,6 +11,13 @@ module Vagrant
# @return [Hash<Symbol, Array>]
attr_reader :action_hooks
# This contains all the command plugins by name, and returns
# the command class and options. The command class is wrapped
# in a Proc so that it can be lazy loaded.
#
# @return [Registry<Symbol, Array<Proc, Hash>>]
attr_reader :commands
# This contains all the configuration plugins by scope.
#
# @return [Hash<Symbol, Registry>]
@ -51,6 +58,7 @@ module Vagrant
# The action hooks hash defaults to []
@action_hooks = Hash.new { |h, k| h[k] = [] }
@commands = Registry.new
@configs = Hash.new { |h, k| h[k] = Registry.new }
@guests = Registry.new
@guest_capabilities = Hash.new { |h, k| h[k] = Registry.new }

View File

@ -81,21 +81,18 @@ module Vagrant
# "vagrant foo" becomes available.
#
# @param [String] name Subcommand key.
def self.command(name=UNSET_VALUE, &block)
data[:command] ||= Registry.new
if name != UNSET_VALUE
# Validate the name of the command
if name.to_s !~ /^[-a-z0-9]+$/i
raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens"
end
# Register a new command class only if a name was given.
data[:command].register(name.to_sym, &block)
def self.command(name, **opts, &block)
# Validate the name of the command
if name.to_s !~ /^[-a-z0-9]+$/i
raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens"
end
# Return the registry
data[:command]
# Register the command
components.commands.register(name.to_sym) do
[block, opts]
end
nil
end
# Defines additional communicators to be available. Communicators

View File

@ -53,7 +53,20 @@ describe Vagrant::Plugin::V2::Plugin do
command("foo") { "bar" }
end
plugin.command[:foo].should == "bar"
expect(plugin.components.commands.keys).to be_include(:foo)
expect(plugin.components.commands[:foo][0].call).to eql("bar")
end
it "should register command classes with options" do
plugin = Class.new(described_class) do
command("foo", opt: :bar) { "bar" }
end
expect(plugin.components.commands.keys).to be_include(:foo)
expect(plugin.components.commands[:foo][0].call).to eql("bar")
expect(plugin.components.commands[:foo][1]).to eql({
opt: :bar,
})
end
["spaces bad", "sym^bols"].each do |bad|
@ -79,8 +92,8 @@ describe Vagrant::Plugin::V2::Plugin do
# Now verify when we actually get the command key that
# a proper error is raised.
expect {
plugin.command[:foo]
}.to raise_error(StandardError)
plugin.components.commands[:foo][0].call
}.to raise_error(StandardError, "FAIL!")
end
end