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) I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root)
# Register the built-in commands # Register the built-in commands
Vagrant.commands.register(:box) { Vagrant::Command::Box }
Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy } Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy }
Vagrant.commands.register(:halt) { Vagrant::Command::Halt } Vagrant.commands.register(:halt) { Vagrant::Command::Halt }
Vagrant.commands.register(:package) { Vagrant::Command::Package } Vagrant.commands.register(:package) { Vagrant::Command::Package }

View File

@ -2,6 +2,11 @@ module Vagrant
module Command module Command
autoload :Base, 'vagrant/command/base' 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 :Destroy, 'vagrant/command/destroy'
autoload :Halt, 'vagrant/command/halt' autoload :Halt, 'vagrant/command/halt'
autoload :Package, 'vagrant/command/package' autoload :Package, 'vagrant/command/package'

View File

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