`vagrant destroy`

This commit is contained in:
Mitchell Hashimoto 2011-12-17 11:05:49 -08:00
parent b292008f3b
commit e71007d47e
5 changed files with 26 additions and 5 deletions

View File

@ -94,6 +94,7 @@ end
I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root)
# Register the built-in commands
Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy }
Vagrant.commands.register(:up) { Vagrant::Command::Up }
# Register the built-in config keys

View File

@ -2,6 +2,7 @@ module Vagrant
module Command
autoload :Base, 'vagrant/command/base'
autoload :Destroy, 'vagrant/command/destroy'
autoload :Up, 'vagrant/command/up'
end
end

View File

@ -31,11 +31,14 @@ module Vagrant
#
# If this method returns `nil`, then you should assume that help
# was printed and parsing failed.
def parse_options(opts)
def parse_options(opts=nil)
# Creating a shallow copy of the arguments so the OptionParser
# doesn't destroy the originals.
argv = @argv.dup
# Default opts to a blank optionparser if none is given
opts ||= OptionParser.new
# Add the help option, which must be on every command.
opts.on_tail("-h", "--help", "Print this help") do
puts opts.help

View File

@ -1,13 +1,24 @@
require 'optparse'
module Vagrant
module Command
class DestroyCommand < NamedBase
register "destroy", "Destroy the environment, deleting the created virtual machines"
class Destroy < Base
def execute
target_vms.each do |vm|
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant destroy [vm-name]"
end
# Parse the options
argv = parse_options(opts)
return if !argv
@logger.debug("'Destroy' each target VM...")
with_target_vms(argv[0]) do |vm|
if vm.created?
@logger.info("Destroying: #{vm.name}")
vm.destroy
else
@logger.info("Not destroying #{vm.name}, since it isn't created.")
vm.env.ui.info I18n.t("vagrant.commands.common.vm_not_created")
end
end

View File

@ -25,6 +25,11 @@ describe Vagrant::Command::Base do
result.should == ["foo"]
end
it "creates an option parser if none is given" do
result = klass.new(["foo"], nil).parse_options(nil)
result.should == ["foo"]
end
["-h", "--help"].each do |help_string|
it "returns nil and prints the help if '#{help_string}' is given" do
instance = klass.new([help_string], nil)