diff --git a/lib/vagrant/commands/base.rb b/lib/vagrant/commands/base.rb index 664a38b03..f58e8e8fa 100644 --- a/lib/vagrant/commands/base.rb +++ b/lib/vagrant/commands/base.rb @@ -12,7 +12,7 @@ module Vagrant attr_reader :env - class < 1 + # There should never be more than 1 arg + show_help + return + end if !options[:global] - show_local_status + show_local_status(*args) else show_global_status end end # Shows the status of the CURRENT environment (the current working - # directory). This prints out a human friendly sentence or paragraph - # describing the state of the Vagrant environment represented by the - # current working directory. - def show_local_status + # directory). If a specific VM was given, it will print out + # detailed information regarding that VM. If no single VM was + # specified and it is a multi-VM environment, it will simply + # show a listing of all the VMs and their short one word + # statuses. + def show_local_status(vm=nil) + if !env.root_path + wrap_output { puts Translator.t(:status_no_environment) } + return + end + + if vm.nil? + if env.multivm? + # No specific VM was specified in a multi-vm environment, + # so show short info for each VM + show_list + else + # Set the VM to just be the root VM + vm = env.vms.values.first + end + else + # Try to get the vm based on the name. If the specified VM + # doesn't exist, then error saying so + vm = env.vms[vm.to_sym] + end + + show_single(vm) + end + + # Lists the available VMs and brief statuses about each. + def show_list + wrap_output do + puts Translator.t(:status_listing) + + env.vms.each do |name, vm| + puts "#{name.ljust(20)}#{vm.state}" + end + end + end + + # Shows a paragraph of information based on the current state of + # a single, specified VM. + def show_single(vm) string_key = nil - if !env.root_path - string_key = :status_no_environment - elsif !env.vm + if !vm.created? string_key = :status_not_created else additional_key = nil - if env.vm.vm.running? + if vm.vm.running? additional_key = :status_created_running - elsif env.vm.vm.saved? + elsif vm.vm.saved? additional_key = :status_created_saved - elsif env.vm.vm.powered_off? + elsif vm.vm.powered_off? additional_key = :status_created_powered_off end string_key = [:status_created, { - :vm_state => env.vm.vm.state, + :vm_state => vm.vm.state, :additional_message => additional_key ? Translator.t(additional_key) : "" }] end - string_key = [string_key, {}] unless string_key.is_a?(Array) wrap_output { puts Translator.t(*string_key) } end @@ -70,6 +111,7 @@ module Vagrant # Defaults options[:global] = false + options[:vm] = nil opts.on("-g", "--global", "Show global status of Vagrant (running VMs managed by Vagrant)") do |v| options[:global] = true @@ -77,4 +119,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index bcd220d95..5e485396b 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -100,6 +100,17 @@ module Vagrant @vms ||= {} end + # Returns a boolean whether this environment represents a multi-VM + # environment or not. This will work even when called on child + # environments. + def multivm? + if parent + parent.multivm? + else + vms.length > 1 + end + end + #--------------------------------------------------------------- # Load Methods #--------------------------------------------------------------- diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 25744163f..51059bd72 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -4,6 +4,7 @@ module Vagrant attr_reader :env attr_reader :system + attr_reader :name attr_accessor :vm class << self @@ -25,6 +26,7 @@ module Vagrant opts = defaults.merge(opts || {}) @vm = opts[:vm] + @name = opts[:vm_name] if !opts[:env].nil? # We have an environment, so we create a new child environment @@ -37,7 +39,7 @@ module Vagrant :vm => self }).load! - # Load the associated system. + # Load the associated system. load_system! end end diff --git a/templates/strings.yml b/templates/strings.yml index 7cac40e82..1f1450582 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -6,6 +6,10 @@ #--------------------------------------------------------------------- # CATEGORY: Status Messages #--------------------------------------------------------------------- +:status_listing: |- + This environment represents multiple VMs. The VMs will be listed + below with a short status. For more detailed information about a + VM, run `vagrant status NAME`. :status_no_environment: |- No vagrant environment detected. Run `vagrant init` to setup a Vagrantfile in the current directory to get started with Vagrant. diff --git a/test/vagrant/commands/base_test.rb b/test/vagrant/commands/base_test.rb index c2a011451..2eca395e3 100644 --- a/test/vagrant/commands/base_test.rb +++ b/test/vagrant/commands/base_test.rb @@ -112,8 +112,9 @@ class CommandsBaseTest < Test::Unit::TestCase end should "parse the options with the args" do - @option_parser.expects(:parse!).with(@args).once - assert_equal @options, @instance.parse_options(@args) + result = mock("result") + @option_parser.expects(:parse!).with(@args).once.returns(result) + assert_equal result, @instance.parse_options(@args) end end end diff --git a/test/vagrant/commands/status_test.rb b/test/vagrant/commands/status_test.rb index f975d5885..f79f66d27 100644 --- a/test/vagrant/commands/status_test.rb +++ b/test/vagrant/commands/status_test.rb @@ -26,5 +26,15 @@ class CommandsStatusTest < Test::Unit::TestCase @instance.expects(:show_global_status).once @instance.execute(["--global"]) end + + should "show help if too many args are given" do + @instance.expects(:show_help).once + @instance.execute(["1","2","3"]) + end + + should "pass the VM name to local status if given" do + @instance.expects(:show_local_status).with("foo").once + @instance.execute(["foo"]) + end end end diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index f82dfd87a..a1baf5a70 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -124,6 +124,41 @@ class EnvironmentTest < Test::Unit::TestCase end end + context "multivm? helper" do + setup do + @env = mock_environment + end + + context "with a parent" do + setup do + @parent = mock('parent') + @env.stubs(:parent).returns(@parent) + end + + should "return the value of multivm? from the parent" do + result = mock("result") + @parent.stubs(:multivm?).returns(result) + assert_equal result, @env.multivm? + end + end + + context "without a parent" do + setup do + @env.stubs(:parent).returns(nil) + end + + should "return true if VM length greater than 1" do + @env.stubs(:vms).returns([1,2,3]) + assert @env.multivm? + end + + should "return false if VM length is 1" do + @env.stubs(:vms).returns([1]) + assert !@env.multivm? + end + end + end + context "loading" do setup do @env = mock_environment diff --git a/vagrant.gemspec b/vagrant.gemspec index d005ca06d..1d545c6af 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= s.authors = ["Mitchell Hashimoto", "John Bender"] - s.date = %q{2010-05-15} + s.date = %q{2010-05-16} s.default_executable = %q{vagrant} s.description = %q{Vagrant is a tool for building and distributing virtualized development environments.} s.email = ["mitchell.hashimoto@gmail.com", "john.m.bender@gmail.com"]