commands/box: List base box downloaded URL and datetime when `-i` gets provided to `box list`

This commit is contained in:
Fabio Rehm 2013-10-04 22:27:30 -03:00
parent cbc7e7eedb
commit 18cf66e83a
3 changed files with 43 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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.