From 8bc36824bf6673a5ed7ea627160bc96bb03850f6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 11 Jan 2014 09:32:13 -0800 Subject: [PATCH] commands/list-commands: lists all commands primary and non-primary --- plugins/commands/list-commands/command.rb | 43 +++++++++++++++++++ plugins/commands/list-commands/plugin.rb | 18 ++++++++ templates/locales/en.yml | 5 +++ test/unit/base.rb | 1 + .../commands/list-commands/command_test.rb | 40 +++++++++++++++++ .../support/shared/plugin_command_context.rb | 11 +++++ test/unit/vagrant/cli_test.rb | 11 +---- 7 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 plugins/commands/list-commands/command.rb create mode 100644 plugins/commands/list-commands/plugin.rb create mode 100644 test/unit/plugins/commands/list-commands/command_test.rb create mode 100644 test/unit/support/shared/plugin_command_context.rb diff --git a/plugins/commands/list-commands/command.rb b/plugins/commands/list-commands/command.rb new file mode 100644 index 000000000..cefc71755 --- /dev/null +++ b/plugins/commands/list-commands/command.rb @@ -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 diff --git a/plugins/commands/list-commands/plugin.rb b/plugins/commands/list-commands/plugin.rb new file mode 100644 index 000000000..53f4a6a82 --- /dev/null +++ b/plugins/commands/list-commands/plugin.rb @@ -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 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 2a80ae186..0d41d8e23 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -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 diff --git a/test/unit/base.rb b/test/unit/base.rb index c9ffe6654..0934e1b9d 100644 --- a/test/unit/base.rb +++ b/test/unit/base.rb @@ -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 diff --git a/test/unit/plugins/commands/list-commands/command_test.rb b/test/unit/plugins/commands/list-commands/command_test.rb new file mode 100644 index 000000000..006396ba8 --- /dev/null +++ b/test/unit/plugins/commands/list-commands/command_test.rb @@ -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 diff --git a/test/unit/support/shared/plugin_command_context.rb b/test/unit/support/shared/plugin_command_context.rb new file mode 100644 index 000000000..e5f5a5612 --- /dev/null +++ b/test/unit/support/shared/plugin_command_context.rb @@ -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 diff --git a/test/unit/vagrant/cli_test.rb b/test/unit/vagrant/cli_test.rb index 047f8ae54..620193eda 100644 --- a/test/unit/vagrant/cli_test.rb +++ b/test/unit/vagrant/cli_test.rb @@ -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)