commands/box: Extract a base class for dealing with StateFile instantiation

This commit is contained in:
Fabio Rehm 2013-10-04 22:02:24 -03:00
parent 1c689d2211
commit 031119a858
3 changed files with 31 additions and 8 deletions

View File

@ -1,9 +1,11 @@
require 'optparse'
require_relative "base"
module VagrantPlugins
module CommandBox
module Command
class Add < Vagrant.plugin("2", :command)
class Add < Base
def execute
options = {}
@ -34,13 +36,12 @@ module VagrantPlugins
provider = nil
provider = options[:provider].to_sym if options[:provider]
@env.action_runner.run(Vagrant::Action.action_box_add, {
action(Vagrant::Action.action_box_add, {
:box_name => argv[0],
:box_provider => provider,
:box_url => argv[1],
:box_force => options[:force],
:box_download_insecure => options[:insecure],
:box_state_file => StateFile.new(@env.home_path.join('boxes.json'))
:box_download_insecure => options[:insecure]
})
# Success, exit status 0

View File

@ -0,0 +1,21 @@
module VagrantPlugins
module CommandBox
module Command
class Base < Vagrant.plugin("2", :command)
# This is a helper for executing an action sequence with the proper
# environment hash setup so that the plugin specific helpers are
# in.
#
# @param [Object] callable the Middleware callable
# @param [Hash] env Extra environment hash that is merged in.
def action(callable, env=nil)
env = {
:box_state_file => StateFile.new(@env.home_path.join("boxes.json"))
}.merge(env || {})
@env.action_runner.run(callable, env)
end
end
end
end
end

View File

@ -1,9 +1,11 @@
require 'optparse'
require_relative "base"
module VagrantPlugins
module CommandBox
module Command
class Remove < Vagrant.plugin("2", :command)
class Remove < Base
def execute
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant box remove <name> <provider>"
@ -34,10 +36,9 @@ module VagrantPlugins
argv[1] = providers[0] || ""
end
@env.action_runner.run(Vagrant::Action.action_box_remove, {
action(Vagrant::Action.action_box_remove, {
:box_name => argv[0],
:box_provider => argv[1],
:box_state_file => StateFile.new(@env.home_path.join('boxes.json'))
:box_provider => argv[1]
})
# Success, exit status 0