From f60b383b7550922c878aad2d1ea6f7b2a44837d1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 22 Feb 2010 16:13:53 -0800 Subject: [PATCH] `vagrant box` command with primitive functionality is in. Doesn't do anything productive yet. --- bin/vagrant-box | 35 +++++++++++++++++++++++++++++ lib/vagrant/actions/box/add.rb | 8 +++++++ lib/vagrant/box.rb | 5 +++++ lib/vagrant/commands.rb | 28 +++++++++++++++++++++++- test/vagrant/commands_test.rb | 40 ++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100755 bin/vagrant-box create mode 100644 lib/vagrant/actions/box/add.rb create mode 100644 lib/vagrant/box.rb diff --git a/bin/vagrant-box b/bin/vagrant-box new file mode 100755 index 000000000..1d05c6a23 --- /dev/null +++ b/bin/vagrant-box @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby +begin + require File.expand_path('../../.bundle/environment', __FILE__) +rescue LoadError + # Fallback on rubygems + require "rubygems" +end + +require 'git-style-binary/command' + +# Get library +libdir = File.join(File.dirname(__FILE__), '..', 'lib') +$:.unshift(libdir) unless $:.include?(libdir) +require 'vagrant' + +GitStyleBinary.command do + short_desc "manage boxes" + banner <<-EOS +Usage: + +#{command.full_name} add name uri +#{command.full_name} remove name + +Add and remove vagrant boxes. + +EOS + + run do |command| + begin + Vagrant::Commands.box(command.argv) + rescue ArgumentError + educate + end + end +end diff --git a/lib/vagrant/actions/box/add.rb b/lib/vagrant/actions/box/add.rb new file mode 100644 index 000000000..ae8179380 --- /dev/null +++ b/lib/vagrant/actions/box/add.rb @@ -0,0 +1,8 @@ +module Vagrant + module Actions + module Box + class Add < Base + end + end + end +end \ No newline at end of file diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb new file mode 100644 index 000000000..bc38c0e79 --- /dev/null +++ b/lib/vagrant/box.rb @@ -0,0 +1,5 @@ +module Vagrant + class Box < Actions::Runner + + end +end \ No newline at end of file diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 941c2f3f8..f6d59ffa9 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -132,9 +132,35 @@ Please specify a target package to unpack and import error VM.execute!(Actions::Up, VM.execute!(Actions::Unpackage, name)) - end + end + + # Manages the `vagrant box` command, allowing the user to add + # and remove boxes. This single command, given an array, determines + # which action to take and calls the respective action method + # (see {box_add} and {box_remove}) + def box(argv) + sub_commands = ["add", "remove"] + + if !sub_commands.include?(argv[0]) + error_and_exit(<<-error) +Please specify a valid action to take on the boxes, either +`add` or `remove`. Examples: + +vagrant box add name uri +vagrant box remove name +error + end + + send("box_#{argv[0]}", *argv[1..-1]) + end + + # Adds a box to the local filesystem, given a URI. + def box_add(name, path) + Box.execute!(Actions::Box::Add, name, path) + end private + def act_on_vm(&block) yield Env.persisted_vm Env.persisted_vm.execute! diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 41c53ac83..e1ad672b1 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -165,4 +165,44 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.package end end + + context "box" do + setup do + Vagrant::Commands.stubs(:box_foo) + Vagrant::Commands.stubs(:box_add) + Vagrant::Commands.stubs(:box_remove) + end + + should "error and exit if the first argument is not 'add' or 'remove'" do + Vagrant::Commands.expects(:error_and_exit).once + Vagrant::Commands.box(["foo"]) + end + + should "not error and exit if the first argument is 'add' or 'remove'" do + commands = ["add", "remove"] + + commands.each do |command| + Vagrant::Commands.expects(:error_and_exit).never + Vagrant::Commands.expects("box_#{command}".to_sym).once + Vagrant::Commands.box([command]) + end + end + + should "forward any additional arguments" do + Vagrant::Commands.expects(:box_add).with(1,2,3).once + Vagrant::Commands.box(["add",1,2,3]) + end + end + + context "box add" do + setup do + @name = "foo" + @path = "bar" + end + + should "execute the add action with the name and path" do + Vagrant::Box.expects(:execute!).with(Vagrant::Actions::Box::Add, @name, @path).once + Vagrant::Commands.box_add(@name, @path) + end + end end