commands/box: Extract box removal code from `box remove` command into a builtin action

This commit is contained in:
Fabio Rehm 2013-10-04 21:54:21 -03:00
parent 1443d634c7
commit e4b5db829e
3 changed files with 54 additions and 14 deletions

View File

@ -8,7 +8,8 @@ module Vagrant
# Builtin contains middleware classes that are shipped with Vagrant-core
# and are thus available to all plugins as a "standard library" of sorts.
module Builtin
autoload :BoxAdd, "vagrant/action/builtin/box_add"
autoload :BoxAdd, "vagrant/action/builtin/box_add"
autoload :BoxRemove, "vagrant/action/builtin/box_remove"
autoload :Call, "vagrant/action/builtin/call"
autoload :Confirm, "vagrant/action/builtin/confirm"
autoload :ConfigValidate, "vagrant/action/builtin/config_validate"
@ -43,5 +44,14 @@ module Vagrant
b.use Builtin::WriteBoxInfo
end
end
# This is the action that will remove a box given a name (and optionally
# a provider). This middleware sequence is built-in to Vagrant. Plugins
# can hook into this like any other middleware sequence.
def self.action_box_remove
Builder.new.tap do |b|
b.use Builtin::BoxRemove
end
end
end
end

View File

@ -0,0 +1,38 @@
require "log4r"
module Vagrant
module Action
module Builtin
# This middleware will remove a box for a given provider.
class BoxRemove
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::action::builtin::box_remove")
end
def call(env)
box_name = env[:box_name]
box_provider = env[:box_provider].to_sym
box = nil
begin
box = env[:box_collection].find(box_name, box_provider)
rescue Vagrant::Errors::BoxUpgradeRequired
@env.boxes.upgrade(box_name)
retry
end
raise Vagrant::Errors::BoxNotFound, :name => box_name, :provider => box_provider if !box
env[:ui].info(I18n.t("vagrant.commands.box.removing",
:name => box_name,
:provider => box_provider))
box.destroy!
# Passes on the removed box to the rest of the middleware chain
env[:box_removed] = box
@app.call(env)
end
end
end
end
end

View File

@ -34,19 +34,11 @@ module VagrantPlugins
argv[1] = providers[0] || ""
end
b = nil
begin
b = @env.boxes.find(argv[0], argv[1].to_sym)
rescue Vagrant::Errors::BoxUpgradeRequired
@env.boxes.upgrade(argv[0])
retry
end
raise Vagrant::Errors::BoxNotFound, :name => argv[0], :provider => argv[1].to_sym if !b
@env.ui.info(I18n.t("vagrant.commands.box.removing",
:name => argv[0],
:provider => argv[1]))
b.destroy!
@env.action_runner.run(Vagrant::Action.action_box_remove, {
:box_name => argv[0],
:box_provider => argv[1],
:box_state_file => StateFile.new(@env.home_path.join('boxes.json'))
})
# Success, exit status 0
0