diff --git a/plugins/commands/box/command/base.rb b/plugins/commands/box/command/base.rb index b80b2dcb7..9b07cfe4d 100644 --- a/plugins/commands/box/command/base.rb +++ b/plugins/commands/box/command/base.rb @@ -10,11 +10,15 @@ module VagrantPlugins # @param [Hash] env Extra environment hash that is merged in. def action(callable, env=nil) env = { - :box_state_file => StateFile.new(@env.home_path.join("boxes.json")) + :box_state_file => box_state_file }.merge(env || {}) @env.action_runner.run(callable, env) end + + def box_state_file + @box_state_file ||= StateFile.new(@env.home_path.join("boxes.json")) + end end end end diff --git a/plugins/commands/box/command/list.rb b/plugins/commands/box/command/list.rb index 2eb4e0790..ca1b0f3c4 100644 --- a/plugins/commands/box/command/list.rb +++ b/plugins/commands/box/command/list.rb @@ -1,14 +1,21 @@ require 'optparse' +require_relative "base" + module VagrantPlugins module CommandBox module Command - class List < Vagrant.plugin("2", :command) + class List < Base def execute options = {} opts = OptionParser.new do |opts| opts.banner = "Usage: vagrant box list" + opts.separator "" + + opts.on("-i", "--box-info", "Displays additional information about the boxes.") do |i| + options[:info] = i + end end # Parse the options @@ -20,6 +27,15 @@ module VagrantPlugins return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false) end + list_boxes(boxes, options[:info]) + + # Success, exit status 0 + 0 + end + + private + + def list_boxes(boxes, extra_info) # Find the longest box name longest_box = boxes.max_by { |x| x[0].length } longest_box_length = longest_box[0].length @@ -33,15 +49,18 @@ module VagrantPlugins # important for the user to know what boxes need to be upgraded # and which don't, since we plan on doing that transparently. boxes.each do |name, provider, _v1| + extra = '' + if extra_info + extra << "\n `- URL: #{box_state_file.box_url(name, provider)}" + extra << "\n `- Date: #{box_state_file.downloaded_at(name, provider)}" + end + name = name.ljust(longest_box_length) provider = "(#{provider})".ljust(longest_provider_length + 2) # 2 -> parenthesis - box_info = "#{name} #{provider}" + box_info = "#{name} #{provider}#{extra}" @env.ui.info(box_info, :prefix => false) end - - # Success, exit status 0 - 0 end end end diff --git a/plugins/commands/box/state_file.rb b/plugins/commands/box/state_file.rb index c0f7c8a1e..0ea690da9 100644 --- a/plugins/commands/box/state_file.rb +++ b/plugins/commands/box/state_file.rb @@ -28,6 +28,20 @@ module VagrantPlugins save! end + def box_url(name, provider) + box_key = "#{name}-#{provider}" + + box_info = @data["boxes"].fetch(box_key, {}) + box_info['url'] || 'Unknown' + end + + def downloaded_at(name, provider) + box_key = "#{name}-#{provider}" + + box_info = @data["boxes"].fetch(box_key, {}) + box_info['downloaded_at'] || 'Unknown' + end + # Remove a box that has been previously downloaded from the state file. # # @param [Box] box The box that was removed.