From e29c5436e1a1835c28431d3fae1ed24871e4064a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 17 Dec 2011 19:52:48 -0800 Subject: [PATCH] Box commands --- lib/vagrant.rb | 1 + lib/vagrant/command.rb | 5 +++ lib/vagrant/command/box.rb | 67 +++++++++++++++++++--------- lib/vagrant/command/box_add.rb | 21 +++++++++ lib/vagrant/command/box_list.rb | 25 +++++++++++ lib/vagrant/command/box_remove.rb | 23 ++++++++++ lib/vagrant/command/box_repackage.rb | 23 ++++++++++ 7 files changed, 144 insertions(+), 21 deletions(-) create mode 100644 lib/vagrant/command/box_add.rb create mode 100644 lib/vagrant/command/box_list.rb create mode 100644 lib/vagrant/command/box_remove.rb create mode 100644 lib/vagrant/command/box_repackage.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index f8ff8694f..5d74f6554 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -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 } diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index cfabf9df6..b8a3fc49e 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -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' diff --git a/lib/vagrant/command/box.rb b/lib/vagrant/command/box.rb index 2f731ad85..d6672e632 100644 --- a/lib/vagrant/command/box.rb +++ b/lib/vagrant/command/box.rb @@ -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 []" + 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 diff --git a/lib/vagrant/command/box_add.rb b/lib/vagrant/command/box_add.rb new file mode 100644 index 000000000..0e149bd21 --- /dev/null +++ b/lib/vagrant/command/box_add.rb @@ -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 " + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + @env.boxes.add(argv[0], argv[1]) + end + end + end +end diff --git a/lib/vagrant/command/box_list.rb b/lib/vagrant/command/box_list.rb new file mode 100644 index 000000000..5adf11c05 --- /dev/null +++ b/lib/vagrant/command/box_list.rb @@ -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 diff --git a/lib/vagrant/command/box_remove.rb b/lib/vagrant/command/box_remove.rb new file mode 100644 index 000000000..dcba60c80 --- /dev/null +++ b/lib/vagrant/command/box_remove.rb @@ -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 " + 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 diff --git a/lib/vagrant/command/box_repackage.rb b/lib/vagrant/command/box_repackage.rb new file mode 100644 index 000000000..f0d632b9b --- /dev/null +++ b/lib/vagrant/command/box_repackage.rb @@ -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 " + 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