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.
75 lines
2.4 KiB
Ruby
75 lines
2.4 KiB
Ruby
require "log4r"
|
|
|
|
module Vagrant
|
|
module Easy
|
|
# Base class for all easy commands. This contains the basic code
|
|
# that knows how to run the easy commands.
|
|
class CommandBase < Vagrant::Command::Base
|
|
# This is the command that this easy command responds to
|
|
attr_reader :command
|
|
|
|
# This is called by the {EasyCommand.create} method when creating
|
|
# an easy command to set the invocation command.
|
|
def self.configure(name, &block)
|
|
# We use class-level instance variables so that each class has
|
|
# its own single command/runner. If we use class variables then this
|
|
# whole base sharse a single one.
|
|
@command = name
|
|
@runner = block
|
|
end
|
|
|
|
def initialize(*args, &block)
|
|
if self.class == CommandBase
|
|
raise "CommandBase must not be instantiated directly. Please subclass."
|
|
end
|
|
|
|
# Let the regular command state setup
|
|
super
|
|
|
|
# Get the command we're listening to and the block we're invoking
|
|
# when we get that command, do some basic validation.
|
|
@command = self.class.instance_variable_get(:@command)
|
|
@runner = self.class.instance_variable_get(:@runner)
|
|
if !@command || !@runner
|
|
raise ArgumentError, "CommandBase requires both a command and a runner"
|
|
end
|
|
|
|
@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 = 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 do |vm|
|
|
@logger.debug("Running easy command for VM: #{vm.name}")
|
|
@runner.call(CommandAPI.new(vm, argv))
|
|
end
|
|
|
|
# Exit status 0 every time for now
|
|
0
|
|
end
|
|
end
|
|
end
|
|
end
|