From e4b5db829effc145bd20db2b201735df64fc5701 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Fri, 4 Oct 2013 21:54:21 -0300 Subject: [PATCH] commands/box: Extract box removal code from `box remove` command into a builtin action --- lib/vagrant/action.rb | 12 +++++++- lib/vagrant/action/builtin/box_remove.rb | 38 ++++++++++++++++++++++++ plugins/commands/box/command/remove.rb | 18 ++++------- 3 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 lib/vagrant/action/builtin/box_remove.rb diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 8c8a241e0..f2fa0e137 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -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 diff --git a/lib/vagrant/action/builtin/box_remove.rb b/lib/vagrant/action/builtin/box_remove.rb new file mode 100644 index 000000000..3ae8bc1ac --- /dev/null +++ b/lib/vagrant/action/builtin/box_remove.rb @@ -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 diff --git a/plugins/commands/box/command/remove.rb b/plugins/commands/box/command/remove.rb index 4cdfcdfa4..bea7dd83b 100644 --- a/plugins/commands/box/command/remove.rb +++ b/plugins/commands/box/command/remove.rb @@ -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