From e14216da4e73df83bf5d34f5e533606865b5a6bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 14 Mar 2014 16:28:54 -0700 Subject: [PATCH] commands/global-status: initial go at it --- plugins/commands/global-status/command.rb | 80 +++++++++++++++++++++++ plugins/commands/global-status/plugin.rb | 18 +++++ templates/locales/en.yml | 11 ++++ 3 files changed, 109 insertions(+) create mode 100644 plugins/commands/global-status/command.rb create mode 100644 plugins/commands/global-status/plugin.rb diff --git a/plugins/commands/global-status/command.rb b/plugins/commands/global-status/command.rb new file mode 100644 index 000000000..a602c344f --- /dev/null +++ b/plugins/commands/global-status/command.rb @@ -0,0 +1,80 @@ +require 'optparse' + +module VagrantPlugins + module CommandGlobalStatus + class Command < Vagrant.plugin("2", :command) + def self.synopsis + "outputs status Vagrant environments for this user" + end + + def execute + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant global-status" + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + columns = [ + ["id", :id], + ["name", :name], + ["provider", :provider], + ["state", :state], + ["directory", :vagrantfile_path], + ] + + widths = {} + widths[:id] = 8 + widths[:name] = 6 + widths[:provider] = 6 + widths[:state] = 6 + widths[:vagrantfile_path] = 35 + + entries = [] + @env.machine_index.each do |entry| + entries << entry + + columns.each do |_, method| + # Skip the id + next if method == :id + + widths[method] ||= 0 + cur = entry.send(method).to_s.length + widths[method] = cur if cur > widths[method] + end + end + + total_width = 0 + columns.each do |header, method| + header = header.ljust(widths[method]) if widths[method] + @env.ui.info("#{header} ", new_line: false) + total_width += header.length + 1 + end + @env.ui.info("") + @env.ui.info("-" * total_width) + + if entries.empty? + @env.ui.info(I18n.t("vagrant.global_status_none")) + return 0 + end + + entries.each do |entry| + columns.each do |_, method| + v = entry.send(method).to_s + v = v[0...7] if method == :id + v = v.ljust(widths[method]) if widths[method] + @env.ui.info("#{v} ", new_line: false) + end + + @env.ui.info("") + end + + @env.ui.info(" \n" + I18n.t("vagrant.global_status_footer")) + + # Success, exit status 0 + 0 + end + end + end +end diff --git a/plugins/commands/global-status/plugin.rb b/plugins/commands/global-status/plugin.rb new file mode 100644 index 000000000..5eb8b21ef --- /dev/null +++ b/plugins/commands/global-status/plugin.rb @@ -0,0 +1,18 @@ +require "vagrant" + +module VagrantPlugins + module CommandGlobalStatus + class Plugin < Vagrant.plugin("2") + name "global-status command" + description <<-DESC + The `global-status` command shows what the running state (running/saved/..) + is of all the Vagrant environments known to the system. + DESC + + command("global-status") do + require File.expand_path("../command", __FILE__) + Command + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a3aaa4bcf..d28cb05a7 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -137,6 +137,17 @@ en: description of what they do. %{list} + global_status_footer: |- + The above shows information about all known Vagrant environments + on this machine. This data is cached and may not be completely + up-to-date. To interact with any of the machines, you can go to + that directory and run Vagrant, or you can use the ID directly + with Vagrant commands from any directory. For example: + "vagrant destroy 1a2b3c4d" + global_status_none: |- + There are no active Vagrant environments on this computer! Or, + you haven't destroyed and recreated Vagrant environments that were + started with an older version of Vagrant. plugin_needs_reinstall: |- The following plugins were installed with a version of Vagrant that had different versions of underlying components. Because