From 9cb53860c2302fd0657c91c906d15a2801f588d2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 1 Jun 2012 14:55:08 +0200 Subject: [PATCH] New easy command APIs: argv, info, error, success Some new APIs were added to the easy command operations. `info`, `error`, and `success` are simple ways to output messages to the UI without resorting to "puts" in Ruby, since the Vagrant UI object is the idiomatic way to do communication with the world. Additionally, `argv` was added which gives commands access to the command-line arguments that are remaining that does not include the vagrant binary or subcommand. Also, behavior was changed: Previously, easy commands would run for every target VM. Now, it is only run once with the primary VM. In the next commit, I plan on adding a new flag that signifies an easy command is meant to work with a named VM. --- lib/vagrant/easy.rb | 1 + lib/vagrant/easy/command_api.rb | 42 ++++++++++++++++++++++++++++++++ lib/vagrant/easy/command_base.rb | 20 ++++++++++++--- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 lib/vagrant/easy/command_api.rb diff --git a/lib/vagrant/easy.rb b/lib/vagrant/easy.rb index 2e3a4caf8..c9410dd24 100644 --- a/lib/vagrant/easy.rb +++ b/lib/vagrant/easy.rb @@ -1,6 +1,7 @@ module Vagrant module Easy autoload :CommandBase, "vagrant/easy/command_base" + autoload :CommandAPI, "vagrant/easy/command_api" autoload :Operations, "vagrant/easy/operations" # This creates a new easy command. This typically is not called diff --git a/lib/vagrant/easy/command_api.rb b/lib/vagrant/easy/command_api.rb new file mode 100644 index 000000000..38b2cd571 --- /dev/null +++ b/lib/vagrant/easy/command_api.rb @@ -0,0 +1,42 @@ +require "delegate" + +require "vagrant/easy/operations" + +module Vagrant + module Easy + # This is the API that easy commands have access to. It is a subclass + # of Operations so it has access to all those methods as well. + class CommandAPI < DelegateClass(Operations) + attr_reader :argv + + def initialize(vm, argv) + super(Operations.new(vm)) + + @argv = argv + @vm = vm + end + + # Outputs an error message to the UI. + # + # @param [String] message Message to send. + def error(message) + @vm.ui.error(message) + end + + # Outputs a normal message to the UI. Use this for any standard-level + # messages. + # + # @param [String] message Message to send. + def info(message) + @vm.ui.info(message) + end + + # Outputs a success message to the UI. + # + # @param [String] message Message to send. + def success(message) + @vm.ui.success(message) + end + end + end +end diff --git a/lib/vagrant/easy/command_base.rb b/lib/vagrant/easy/command_base.rb index ab558afda..85a19ba22 100644 --- a/lib/vagrant/easy/command_base.rb +++ b/lib/vagrant/easy/command_base.rb @@ -44,14 +44,26 @@ module Vagrant end # Parse the options - argv = parse_options(opts) - return if !argv + argv = nil + begin + argv = parse_options(opts) + rescue Errors::CLIInvalidOptions + # This means that an invalid flag such as "--foo" was passed. + # We usually show the help at this point (in built-in commands), + # but since we don't know what our implementation does, we just + # pass the flags through now. + argv = @argv.dup + end + + # If argv is nil then `parse_options` halted execution and we + # halt our own execution here. + return 0 if !argv # Run the action for each VM. @logger.info("Running easy command: #{@command}") - with_target_vms(argv) do |vm| + with_target_vms do |vm| @logger.debug("Running easy command for VM: #{vm.name}") - @runner.call(Operations.new(vm)) + @runner.call(CommandAPI.new(vm, argv)) end # Exit status 0 every time for now