Add `--force` flag to `box add` [GH-631]

This commit is contained in:
Mitchell Hashimoto 2012-01-10 20:37:33 -08:00
parent fe012e800f
commit 0c0d456db2
4 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -8,12 +8,24 @@ module Vagrant
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant box add <name> <url>"
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

View File

@ -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