`vagrant status`

This commit is contained in:
Mitchell Hashimoto 2010-08-24 17:23:46 -07:00
parent 82ee787229
commit 2f2ac59dd5
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,62 @@
module Vagrant
module Command
class StatusCommand < Base
desc "Shows the status of the current Vagrant environment."
argument :name, :type => :string, :optional => true
register "status"
def verify_environment
raise NoEnvironmentError.new("No Vagrant environment detected. Run `vagrant init` to set one up.") if !env.root_path
end
def route
if env.multivm?
return show_mulitvm if !self.name
vm = env.vms[self.name.to_sym]
raise VMNotFoundError.new("A VM by the name of `#{self.name}` was not found.") if !vm
else
raise MultiVMEnvironmentRequired.new("A multi-vm environment is required for name specification to a command.") if self.name
vm = env.vms.values.first
end
show_single(vm)
end
protected
def show_multivm
puts Util::Translator.t(:status_listing)
puts ""
env.vms.each do |name, vm|
state = vm.created? ? vm.vm.state : "not created"
puts "#{name.to_s.ljust(30)}#{state}"
end
end
def show_single(vm)
string_key = nil
if !vm.created?
string_key = :status_not_created
else
additional_key = nil
if vm.vm.running?
additional_key = :status_created_running
elsif vm.vm.saved?
additional_key = :status_created_saved
elsif vm.vm.powered_off?
additional_key = :status_created_powered_off
end
string_key = [:status_created, {
:vm_state => vm.vm.state,
:additional_message => additional_key ? Translator.t(additional_key) : ""
}]
end
puts Util::Translator.t(*string_key)
end
end
end
end

View File

@ -7,4 +7,7 @@ module Vagrant
class CLIMissingEnvironment < VagrantError; status_code(1); end
class BoxNotFound < VagrantError; status_code(2); end
class NoEnvironmentError < VagrantError; status_code(3); end
class VMNotFoundError < VagrantError; status_code(4); end
class MultiVMEnvironmentRequired < VagrantError; status_code(5); end
end