core: allow command plugins to have options
This commit is contained in:
parent
6a7e07c53f
commit
4a99cdccdf
|
@ -11,6 +11,13 @@ module Vagrant
|
||||||
# @return [Hash<Symbol, Array>]
|
# @return [Hash<Symbol, Array>]
|
||||||
attr_reader :action_hooks
|
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.
|
# This contains all the configuration plugins by scope.
|
||||||
#
|
#
|
||||||
# @return [Hash<Symbol, Registry>]
|
# @return [Hash<Symbol, Registry>]
|
||||||
|
@ -51,6 +58,7 @@ module Vagrant
|
||||||
# The action hooks hash defaults to []
|
# The action hooks hash defaults to []
|
||||||
@action_hooks = Hash.new { |h, k| h[k] = [] }
|
@action_hooks = Hash.new { |h, k| h[k] = [] }
|
||||||
|
|
||||||
|
@commands = Registry.new
|
||||||
@configs = Hash.new { |h, k| h[k] = Registry.new }
|
@configs = Hash.new { |h, k| h[k] = Registry.new }
|
||||||
@guests = Registry.new
|
@guests = Registry.new
|
||||||
@guest_capabilities = Hash.new { |h, k| h[k] = Registry.new }
|
@guest_capabilities = Hash.new { |h, k| h[k] = Registry.new }
|
||||||
|
|
|
@ -81,21 +81,18 @@ module Vagrant
|
||||||
# "vagrant foo" becomes available.
|
# "vagrant foo" becomes available.
|
||||||
#
|
#
|
||||||
# @param [String] name Subcommand key.
|
# @param [String] name Subcommand key.
|
||||||
def self.command(name=UNSET_VALUE, &block)
|
def self.command(name, **opts, &block)
|
||||||
data[:command] ||= Registry.new
|
# Validate the name of the command
|
||||||
|
if name.to_s !~ /^[-a-z0-9]+$/i
|
||||||
if name != UNSET_VALUE
|
raise InvalidCommandName, "Commands can only contain letters, numbers, and hyphens"
|
||||||
# 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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the registry
|
# Register the command
|
||||||
data[:command]
|
components.commands.register(name.to_sym) do
|
||||||
|
[block, opts]
|
||||||
|
end
|
||||||
|
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defines additional communicators to be available. Communicators
|
# Defines additional communicators to be available. Communicators
|
||||||
|
|
|
@ -53,7 +53,20 @@ describe Vagrant::Plugin::V2::Plugin do
|
||||||
command("foo") { "bar" }
|
command("foo") { "bar" }
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
["spaces bad", "sym^bols"].each do |bad|
|
["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
|
# Now verify when we actually get the command key that
|
||||||
# a proper error is raised.
|
# a proper error is raised.
|
||||||
expect {
|
expect {
|
||||||
plugin.command[:foo]
|
plugin.components.commands[:foo][0].call
|
||||||
}.to raise_error(StandardError)
|
}.to raise_error(StandardError, "FAIL!")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue