commands/list-commands: lists all commands primary and non-primary

This commit is contained in:
Mitchell Hashimoto 2014-01-11 09:32:13 -08:00
parent 08b1aee00d
commit 8bc36824bf
7 changed files with 119 additions and 10 deletions

View File

@ -0,0 +1,43 @@
require "optparse"
module VagrantPlugins
module CommandListCommands
class Command < Vagrant.plugin("2", :command)
def self.synopsis
"outputs all available Vagrant subcommands, even non-primary ones"
end
def execute
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant list-commands"
end
argv = parse_options(opts)
return if !argv
# Add the available subcommands as separators in order to print them
# out as well.
commands = {}
longest = 0
Vagrant.plugin("2").manager.commands.each do |key, data|
key = key.to_s
klass = data[0].call
commands[key] = klass.synopsis
longest = key.length if key.length > longest
end
command_output = []
commands.keys.sort.each do |key|
command_output << "#{key.ljust(longest+2)} #{commands[key]}"
@env.ui.machine("cli-command", key.dup)
end
@env.ui.info(
I18n.t("vagrant.list_commands", list: command_output.join("\n")))
# Success, exit status 0
0
end
end
end
end

View File

@ -0,0 +1,18 @@
require "vagrant"
module VagrantPlugins
module CommandListCommands
class Plugin < Vagrant.plugin("2")
name "list-commands command"
description <<-DESC
The `list-commands` command will list all commands that Vagrant
understands, even hidden ones.
DESC
command("list-commands", primary: false) do
require_relative "command"
Command
end
end
end
end

View File

@ -61,6 +61,11 @@ en:
Key inserted! Disconnecting and reconnecting using new SSH key...
inserting_insecure_key: |-
Inserting Vagrant public key within guest...
list_commands: |-
Below is a listing of all available Vagrant commands and a brief
description of what they do.
%{list}
plugin_needs_reinstall: |-
The following plugins were installed with a version of Vagrant
that had different versions of underlying components. Because

View File

@ -15,6 +15,7 @@ require "unit/support/dummy_provider"
require "unit/support/shared/base_context"
require "unit/support/shared/action_synced_folders_context"
require "unit/support/shared/capability_helpers_context"
require "unit/support/shared/plugin_command_context"
require "unit/support/shared/virtualbox_context"
# Do not buffer output

View File

@ -0,0 +1,40 @@
require File.expand_path("../../../../base", __FILE__)
require Vagrant.source_root.join("plugins/commands/list-commands/command")
describe VagrantPlugins::CommandListCommands::Command do
include_context "unit"
include_context "command plugin helpers"
let(:iso_env) do
# We have to create a Vagrantfile so there is a root path
env = isolated_environment
env.vagrantfile("")
env.create_vagrant_env
end
let(:argv) { [] }
let(:commands) { {} }
subject { described_class.new(argv, iso_env) }
before do
Vagrant.plugin("2").manager.stub(commands: commands)
end
describe "execute" do
it "includes all subcommands" do
commands[:foo] = [command_lambda("foo", 0), { primary: true }]
commands[:bar] = [command_lambda("bar", 0), { primary: true }]
commands[:baz] = [command_lambda("baz", 0), { primary: false }]
iso_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.execute
end
end
end

View File

@ -0,0 +1,11 @@
shared_context "command plugin helpers" do
def command_lambda(name, result)
lambda do
Class.new(Vagrant.plugin("2", "command")) do
define_method(:execute) do
result
end
end
end
end
end

View File

@ -4,6 +4,7 @@ require "vagrant/cli"
describe Vagrant::CLI do
include_context "unit"
include_context "command plugin helpers"
let(:commands) { {} }
let(:iso_env) { isolated_environment }
@ -13,16 +14,6 @@ describe Vagrant::CLI 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)