Box commands

This commit is contained in:
Mitchell Hashimoto 2011-12-17 19:52:48 -08:00
parent 780722386b
commit e29c5436e1
7 changed files with 144 additions and 21 deletions

View File

@ -94,6 +94,7 @@ end
I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root)
# Register the built-in commands
Vagrant.commands.register(:box) { Vagrant::Command::Box }
Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy }
Vagrant.commands.register(:halt) { Vagrant::Command::Halt }
Vagrant.commands.register(:package) { Vagrant::Command::Package }

View File

@ -2,6 +2,11 @@ module Vagrant
module Command
autoload :Base, 'vagrant/command/base'
autoload :Box, 'vagrant/command/box'
autoload :BoxAdd, 'vagrant/command/box_add'
autoload :BoxRemove, 'vagrant/command/box_remove'
autoload :BoxRepackage, 'vagrant/command/box_repackage'
autoload :BoxList, 'vagrant/command/box_list'
autoload :Destroy, 'vagrant/command/destroy'
autoload :Halt, 'vagrant/command/halt'
autoload :Package, 'vagrant/command/package'

View File

@ -1,32 +1,57 @@
require 'optparse'
module Vagrant
module Command
class BoxCommand < GroupBase
register "box", "Commands to manage system boxes"
class Box < Base
def initialize(argv, env)
super
desc "add NAME URI", "Add a box to the system"
def add(name, uri)
env.boxes.add(name, uri)
@main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
@subcommands = Registry.new
@subcommands.register(:add) { Vagrant::Command::BoxAdd }
@subcommands.register(:remove) { Vagrant::Command::BoxRemove }
@subcommands.register(:repackage) { Vagrant::Command::BoxRepackage }
@subcommands.register(:list) { Vagrant::Command::BoxList }
end
desc "remove NAME", "Remove a box from the system"
def remove(name)
b = env.boxes.find(name)
raise Errors::BoxNotFound, :name => name if !b
b.destroy
def execute
if @main_args.include?("-h") || @main_args.include?("--help")
# Print the help for all the box commands.
return help
end
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
return help if !command_class || !@sub_command
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
# Initialize and execute the command class
command_class.new(@sub_args, @env).execute
end
desc "repackage NAME", "Repackage an installed box into a `.box` file."
def repackage(name)
b = env.boxes.find(name)
raise Errors::BoxNotFound, :name => name if !b
b.repackage
end
# Prints the help out for this command
def help
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box <command> [<args>]"
opts.separator ""
opts.separator "Available subcommands:"
desc "list", "Lists all installed boxes"
def list
boxes = env.boxes.sort
return env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false) if boxes.empty?
boxes.each { |b| env.ui.info(b.name, :prefix => false) }
# Add the available subcommands as separators in order to print them
# out as well.
keys = []
@subcommands.each { |key, value| keys << key }
keys.sort.each do |key|
opts.separator " #{key}"
end
opts.separator ""
opts.separator "For help on any individual command run `vagrant box COMMAND -h`"
end
@env.ui.info(opts.help, :prefix => false)
end
end
end

View File

@ -0,0 +1,21 @@
require 'optparse'
module Vagrant
module Command
class BoxAdd < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box add <name> <url>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
@env.boxes.add(argv[0], argv[1])
end
end
end
end

View File

@ -0,0 +1,25 @@
require 'optparse'
module Vagrant
module Command
class BoxList < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box list"
end
# Parse the options
argv = parse_options(opts)
return if !argv
boxes = @env.boxes.sort
if boxes.empty?
return @env.ui.warn(I18n.t("vagrant.commands.box.no_installed_boxes"), :prefix => false)
end
boxes.each { |b| @env.ui.info(b.name, :prefix => false) }
end
end
end
end

View File

@ -0,0 +1,23 @@
require 'optparse'
module Vagrant
module Command
class BoxRemove < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box remove <name>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
b = @env.boxes.find(argv[0])
raise Errors::BoxNotFound, :name => argv[0] if !b
b.destroy
end
end
end
end

View File

@ -0,0 +1,23 @@
require 'optparse'
module Vagrant
module Command
class BoxRepackage < Base
def execute
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box repackage <name>"
end
# Parse the options
argv = parse_options(opts)
return if !argv
b = @env.boxes.find(argv[0])
raise Errors::BoxNotFound, :name => argv[0] if !b
b.repackage
end
end
end
end