vagrant/plugins/commands/box/command/list.rb

72 lines
2.1 KiB
Ruby
Raw Normal View History

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 = {}
2013-11-25 21:26:46 +00:00
opts = OptionParser.new do |o|
2014-02-08 08:20:50 +00:00
o.banner = "Usage: vagrant box list [options]"
o.separator ""
o.separator "Options:"
2013-11-25 21:26:46 +00:00
o.separator ""
2014-02-08 08:20:50 +00:00
o.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
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
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
# 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, version, provider|
@env.ui.info("#{name.ljust(longest_box_length)} (#{provider})")
@env.ui.machine("box-name", name)
@env.ui.machine("box-provider", provider)
@env.ui.machine("box-version", version)
info_file = @env.boxes.find(name, provider, version).
directory.join("info.json")
2013-11-25 21:26:46 +00:00
if info_file.file?
info = JSON.parse(info_file.read)
info.each do |k, v|
@env.ui.machine("box-info", k, v)
2013-11-25 21:26:46 +00:00
if extra_info
@env.ui.info(" - #{k}: #{v}", prefix: false)
end
end
end
end
2012-04-19 20:59:48 +00:00
end
end
end
end
end