2012-04-19 20:59:48 +00:00
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandBox
|
|
|
|
module Command
|
2012-11-07 05:05:14 +00:00
|
|
|
class List < Vagrant.plugin("2", :command)
|
2012-04-19 20:59:48 +00:00
|
|
|
def execute
|
|
|
|
options = {}
|
|
|
|
|
|
|
|
opts = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: vagrant box list"
|
2013-10-05 01:27:30 +00:00
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("-i", "--box-info", "Displays additional information about the boxes.") do |i|
|
|
|
|
options[:info] = i
|
|
|
|
end
|
2012-04-19 20:59:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
2012-07-10 01:37:38 +00:00
|
|
|
boxes = @env.boxes.all.sort
|
2012-04-19 20:59:48 +00:00
|
|
|
if boxes.empty?
|
|
|
|
return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false)
|
|
|
|
end
|
2012-07-10 01:37:38 +00:00
|
|
|
|
2013-10-05 01:27:30 +00:00
|
|
|
list_boxes(boxes, options[:info])
|
|
|
|
|
|
|
|
# Success, exit status 0
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def list_boxes(boxes, extra_info)
|
2012-07-10 01:37:38 +00:00
|
|
|
# Find the longest box name
|
|
|
|
longest_box = boxes.max_by { |x| x[0].length }
|
|
|
|
longest_box_length = longest_box[0].length
|
|
|
|
|
2013-10-05 01:08:39 +00:00
|
|
|
# Find the longest provider name
|
|
|
|
longest_provider = boxes.max_by { |x| x[1].length }
|
|
|
|
longest_provider_length = longest_provider[1].length
|
|
|
|
|
2012-07-10 01:37:38 +00:00
|
|
|
# Go through each box and output the information about it. We
|
|
|
|
# ignore the "v1" param for now since I'm not yet sure if its
|
|
|
|
# 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|
|
|
|
|
@env.ui.info("#{name.ljust(longest_box_length)} (#{provider})", :prefix => false)
|
2013-11-24 19:42:24 +00:00
|
|
|
|
|
|
|
@env.ui.machine("box-name", name)
|
|
|
|
@env.ui.machine("box-provider", provider)
|
2013-11-25 21:20:26 +00:00
|
|
|
|
2013-10-05 01:27:30 +00:00
|
|
|
extra = ''
|
|
|
|
if extra_info
|
2013-10-18 20:57:24 +00:00
|
|
|
extra << format_extra_info(name.to_s, provider.to_s)
|
2013-10-05 01:27:30 +00:00
|
|
|
end
|
|
|
|
|
2013-10-05 01:08:39 +00:00
|
|
|
name = name.ljust(longest_box_length)
|
|
|
|
provider = "(#{provider})".ljust(longest_provider_length + 2) # 2 -> parenthesis
|
2013-10-05 01:27:30 +00:00
|
|
|
box_info = "#{name} #{provider}#{extra}"
|
2013-10-05 01:08:39 +00:00
|
|
|
|
|
|
|
@env.ui.info(box_info, :prefix => false)
|
2012-07-10 01:37:38 +00:00
|
|
|
end
|
2012-04-19 20:59:48 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:57:24 +00:00
|
|
|
def format_extra_info(name, provider)
|
|
|
|
info_json = @env.boxes.find(name, provider).directory.join('info.json')
|
|
|
|
if info_json.file?
|
|
|
|
info = JSON.parse(info_json.read)
|
|
|
|
return "\n `- URL: #{info['url']}" +
|
|
|
|
"\n `- Date: #{info['downloaded_at']}"
|
|
|
|
else
|
|
|
|
return ''
|
|
|
|
end
|
2012-04-19 20:59:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|