From 00aba5ac03435068b3817dec0ce05e061568e7b3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 5 May 2012 18:57:29 -0700 Subject: [PATCH] Plugin easy commands. Easy commands are well... easy! They don't offer the full power of creating a completely custom command class, but they let you do the basics (what almost everyone needs) with minimal fuss. Example: class MyPlugin < Vagrant.plugin("1") name "my-plugin" easy_command "foo" do |action| puts "HELLO!" end end NOTE: The "action" stuff isn't done yet, but will be soon! --- lib/vagrant.rb | 1 + lib/vagrant/easy_command.rb | 57 +++++++++++++++++++++++++++++ lib/vagrant/plugin/v1.rb | 11 ++++++ plugins/commands/reload/command.rb | 2 +- test/unit/vagrant/plugin/v1_test.rb | 11 ++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 lib/vagrant/easy_command.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index b65902863..265ca8922 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -66,6 +66,7 @@ module Vagrant autoload :DataStore, 'vagrant/data_store' autoload :Downloaders, 'vagrant/downloaders' autoload :Driver, 'vagrant/driver' + autoload :EasyCommand, 'vagrant/easy_command' autoload :Environment, 'vagrant/environment' autoload :Errors, 'vagrant/errors' autoload :Guest, 'vagrant/guest' diff --git a/lib/vagrant/easy_command.rb b/lib/vagrant/easy_command.rb new file mode 100644 index 000000000..5c380da44 --- /dev/null +++ b/lib/vagrant/easy_command.rb @@ -0,0 +1,57 @@ +require "log4r" + +module Vagrant + class EasyCommand + # This creates a new easy command. This typically is not called + # directly. Instead, the plugin interface's `easy_command` is + # used to create one of these. + def self.create(name, &block) + # Create a new command class for this command, and return it + command = Class.new(Base) + command.configure(name, &block) + command + end + + # Base class for all easy commands. This contains the basic code + # that knows how to run the easy commands. + class Base < Vagrant::Command::Base + @@command = nil + @@runner = nil + + # This is called by the {EasyCommand.create} method when creating + # an easy command to set the invocation command. + def self.configure(name, &block) + @@command = name + @@runner = block + end + + def initialize(*args, &block) + super + + @logger = Log4r::Logger.new("vagrant::easy_command::#{@@command}") + end + + def execute + # Build up a basic little option parser + opts = OptionParser.new do |opts| + opts.banner = "Usage: vagrant #{@@command}" + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + # Run the action for each VM. + @logger.info("Running easy command: #{@@command}") + with_target_vms(argv) do |vm| + @logger.debug("Running easy command for VM: #{vm.name}") + + @@runner.call(nil) + end + + # Exit status 0 every time for now + 0 + end + end + end +end diff --git a/lib/vagrant/plugin/v1.rb b/lib/vagrant/plugin/v1.rb index bcb9a2c68..3d3737ac6 100644 --- a/lib/vagrant/plugin/v1.rb +++ b/lib/vagrant/plugin/v1.rb @@ -91,6 +91,17 @@ module Vagrant data[:config] end + # Defines an "easy command," which is a command with limited + # functionality but far less boilerplate required over traditional + # commands. Easy commands let you make basic commands quickly and + # easily. + # + # @param [String] name Name of the command, how it will be invoked + # on the command line. + def self.easy_command(name, &block) + command(name) { EasyCommand.create(name, &block) } + end + # Defines an additionally available guest implementation with # the given key. # diff --git a/plugins/commands/reload/command.rb b/plugins/commands/reload/command.rb index ce1194185..abd1628dc 100644 --- a/plugins/commands/reload/command.rb +++ b/plugins/commands/reload/command.rb @@ -10,7 +10,7 @@ module VagrantPlugins def execute options = {} - opts = OptionParser.new do |opts| + opts = OptionParser.new do |opts| opts.banner = "Usage: vagrant reload [vm-name]" opts.separator "" build_start_options(opts, options) diff --git a/test/unit/vagrant/plugin/v1_test.rb b/test/unit/vagrant/plugin/v1_test.rb index ce95e7c08..a73a9246f 100644 --- a/test/unit/vagrant/plugin/v1_test.rb +++ b/test/unit/vagrant/plugin/v1_test.rb @@ -91,6 +91,17 @@ describe Vagrant::Plugin::V1 do end end + describe "easy commands" do + it "should register with the commands" do + plugin = Class.new(described_class) do + easy_command("foo") {} + end + + # Check that the command class subclasses the easy command base + plugin.command[:foo].should < Vagrant::EasyCommand::Base + end + end + describe "guests" do it "should register guest classes" do plugin = Class.new(described_class) do