From 0c0d456db21af8e9583ef473ae3baedb46566a10 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 10 Jan 2012 20:37:33 -0800 Subject: [PATCH] Add `--force` flag to `box add` [GH-631] --- CHANGELOG.md | 2 ++ lib/vagrant/action/box/destroy.rb | 4 ++++ lib/vagrant/command/box_add.rb | 12 ++++++++++++ test/acceptance/box_test.rb | 10 ++++++++++ 4 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e7f06d66..24434e19d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ port of the virtual machine. - If a shared folder now has a `:create` flag set to `true`, the path on the host will be created if it doesn't exist. + - Added `--force` flag to `box add`, which will overwite any existing boxes + if they exist. [GH-631] - Removed Thor as a dependency for the command line interfaces. This resulted in general speed increases across all command line commands. - Linux uses `shutdown -h` instead of `halt` to hopefully more consistently diff --git a/lib/vagrant/action/box/destroy.rb b/lib/vagrant/action/box/destroy.rb index 8f4c3bfc8..d06d9ea07 100644 --- a/lib/vagrant/action/box/destroy.rb +++ b/lib/vagrant/action/box/destroy.rb @@ -10,9 +10,13 @@ module Vagrant end def call(env) + # Delete the existing box env[:ui].info I18n.t("vagrant.actions.box.destroy.destroying", :name => env[:box_name]) FileUtils.rm_rf(env[:box_directory]) + # Reload the box collection + env[:box_collection].reload! + @app.call(env) end end diff --git a/lib/vagrant/command/box_add.rb b/lib/vagrant/command/box_add.rb index 0e149bd21..9c4c029ce 100644 --- a/lib/vagrant/command/box_add.rb +++ b/lib/vagrant/command/box_add.rb @@ -8,12 +8,24 @@ module Vagrant opts = OptionParser.new do |opts| opts.banner = "Usage: vagrant box add " + opts.separator "" + + opts.on("-f", "--force", "Overwrite an existing box if it exists.") do |f| + options[:force] = f + end end # Parse the options argv = parse_options(opts) return if !argv + # If we're force adding, then be sure to destroy any existing box if it + # exists. + if options[:force] + existing = @env.boxes.find(argv[0]) + existing.destroy if existing + end + @env.boxes.add(argv[0], argv[1]) end end diff --git a/test/acceptance/box_test.rb b/test/acceptance/box_test.rb index 4e9b11a6a..c33a789e4 100644 --- a/test/acceptance/box_test.rb +++ b/test/acceptance/box_test.rb @@ -32,6 +32,16 @@ describe "vagrant box" do result.stderr.should match_output(:box_already_exists, "foo") end + it "overwrites a box when adding with `--force`" do + require_box("default") + + # Add the box, which we expect to succeed + assert_execute("vagrant", "box", "add", "foo", box_path("default")) + + # Adding it again should not succeed + assert_execute("vagrant", "box", "add", "foo", box_path("default"), "--force") + end + it "gives an error if the file doesn't exist" do result = execute("vagrant", "box", "add", "foo", "/tmp/nope/nope/nope/nonono.box") result.should_not succeed