core: CLI understands new plugin definition format
This commit is contained in:
parent
4a99cdccdf
commit
ad99b4f250
|
@ -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,8 +59,9 @@ 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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
end
|
||||||
|
|
||||||
let(:environment) do
|
def command_lambda(name, result)
|
||||||
ui = double("UI::Silent")
|
lambda do
|
||||||
ui.stub(:machine => "bar")
|
Class.new(Vagrant.plugin("2", "command")) do
|
||||||
ui.stub(:info => "bar")
|
define_method(:execute) do
|
||||||
env = double("Vagrant::Environment")
|
result
|
||||||
env.stub(:active_machines => [])
|
end
|
||||||
env.stub(:ui => ui)
|
end
|
||||||
env.stub(:root_path => "foo")
|
end
|
||||||
env.stub(:machine_names => [])
|
|
||||||
env
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns a non-zero exit status if an invalid command is given" do
|
describe "#execute" do
|
||||||
result = klass.new(["destroypp"], environment).execute
|
it "invokes help and exits with 1 if invalid command" do
|
||||||
result.should_not == 0
|
subject = described_class.new(["i-dont-exist"], env)
|
||||||
|
subject.should_receive(:help).once
|
||||||
|
expect(subject.execute).to eql(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns an exit status of zero if a valid command is given" do
|
it "invokes command and returns its exit status if the command is valid" do
|
||||||
result = klass.new(["destroy"], environment).execute
|
commands[:destroy] = [command_lambda("destroy", 42), {}]
|
||||||
result.should == 0
|
|
||||||
|
subject = described_class.new(["destroy"], env)
|
||||||
|
subject.should_not_receive(:help)
|
||||||
|
expect(subject.execute).to eql(42)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#help" do
|
||||||
|
subject { described_class.new([], env) }
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue