core: CLI understands new plugin definition format

This commit is contained in:
Mitchell Hashimoto 2014-01-11 08:53:25 -08:00
parent 4a99cdccdf
commit ad99b4f250
3 changed files with 61 additions and 28 deletions

View File

@ -23,15 +23,17 @@ module Vagrant
# If we reached this far then we must have a subcommand. If not, # If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit. # then we also just print the help and exit.
command_class = nil command_plugin = nil
if @sub_command if @sub_command
command_class = Vagrant.plugin("2").manager.commands[@sub_command.to_sym] command_plugin = Vagrant.plugin("2").manager.commands[@sub_command.to_sym]
end end
if !command_class || !@sub_command if !command_plugin || !@sub_command
help help
return 1 return 1
end end
command_class = command_plugin[0].call
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}") @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
# Initialize and execute the command class, returning the exit status. # Initialize and execute the command class, returning the exit status.
@ -57,10 +59,11 @@ module Vagrant
# out as well. # out as well.
commands = {} commands = {}
longest = 0 longest = 0
Vagrant.plugin("2").manager.commands.each do |key, klass| Vagrant.plugin("2").manager.commands.each do |key, data|
key = key.to_s key = key.to_s
klass = data[0].call
commands[key] = klass.synopsis commands[key] = klass.synopsis
longest = key.length if key.length > longest longest = key.length if key.length > longest
end end
commands.keys.sort.each do |key| commands.keys.sort.each do |key|

View File

@ -30,11 +30,11 @@ module Vagrant
# This returns all the registered commands. # This returns all the registered commands.
# #
# @return [Hash] # @return [Registry<Symbol, Array<Proc, Hash>>]
def commands def commands
Registry.new.tap do |result| Registry.new.tap do |result|
@registered.each do |plugin| @registered.each do |plugin|
result.merge!(plugin.command) result.merge!(plugin.components.commands)
end end
end end
end end

View File

@ -1,29 +1,59 @@
require_relative "../base"
require "vagrant/cli"
describe Vagrant::CLI do describe Vagrant::CLI do
describe "parsing options" do include_context "unit"
let(:klass) do
Class.new(described_class) let(:commands) { {} }
let(:iso_env) { isolated_environment }
let(:env) { iso_env.create_vagrant_env }
before do
Vagrant.plugin("2").manager.stub(commands: commands)
end
def command_lambda(name, result)
lambda do
Class.new(Vagrant.plugin("2", "command")) do
define_method(:execute) do
result
end
end
end
end
describe "#execute" do
it "invokes help and exits with 1 if invalid command" do
subject = described_class.new(["i-dont-exist"], env)
subject.should_receive(:help).once
expect(subject.execute).to eql(1)
end end
let(:environment) do it "invokes command and returns its exit status if the command is valid" do
ui = double("UI::Silent") commands[:destroy] = [command_lambda("destroy", 42), {}]
ui.stub(:machine => "bar")
ui.stub(:info => "bar")
env = double("Vagrant::Environment")
env.stub(:active_machines => [])
env.stub(:ui => ui)
env.stub(:root_path => "foo")
env.stub(:machine_names => [])
env
end
it "returns a non-zero exit status if an invalid command is given" do subject = described_class.new(["destroy"], env)
result = klass.new(["destroypp"], environment).execute subject.should_not_receive(:help)
result.should_not == 0 expect(subject.execute).to eql(42)
end end
end
it "returns an exit status of zero if a valid command is given" do describe "#help" do
result = klass.new(["destroy"], environment).execute subject { described_class.new([], env) }
result.should == 0
it "includes all available subcommands" do
commands[:foo] = [command_lambda("foo", 0), {}]
commands[:bar] = [command_lambda("bar", 0), {}]
commands[:baz] = [command_lambda("baz", 0), {}]
env.ui.should_receive(:info).with do |message, opts|
expect(message).to include("foo")
expect(message).to include("bar")
expect(message).to include("baz")
end
subject.help
end end
end end
end end