Reduce fire trigger methods to a single method

This commit is contained in:
Brian Cain 2018-03-29 10:42:06 -07:00
parent eac2fcf71e
commit bbf4e3c521
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
2 changed files with 18 additions and 23 deletions

View File

@ -160,7 +160,7 @@ module Vagrant
# as extra data set on the environment hash for the middleware # as extra data set on the environment hash for the middleware
# runner. # runner.
def action(name, opts=nil) def action(name, opts=nil)
@triggers.fire_before_triggers(name, @name.to_s) @triggers.fire_triggers(name, :before, @name.to_s)
@logger.info("Calling action: #{name} on provider #{@provider}") @logger.info("Calling action: #{name} on provider #{@provider}")
opts ||= {} opts ||= {}
@ -206,7 +206,7 @@ module Vagrant
action_result action_result
end end
@triggers.fire_after_triggers(name, @name.to_s) @triggers.fire_triggers(name, :after, @name.to_s)
rescue Errors::EnvironmentLockedError rescue Errors::EnvironmentLockedError
raise Errors::MachineActionLockedError, raise Errors::MachineActionLockedError,
action: name, action: name,

View File

@ -23,36 +23,31 @@ module Vagrant
@logger = Log4r::Logger.new("vagrant::trigger::#{self.class.to_s.downcase}") @logger = Log4r::Logger.new("vagrant::trigger::#{self.class.to_s.downcase}")
end end
# Fires all before triggers, if any are defined for the action and guest # Fires all triggers, if any are defined for the action and guest
# #
# @param [Symbol] action Vagrant command to fire trigger on # @param [Symbol] action Vagrant command to fire trigger on
# @param [Symbol] stage :before or :after
# @param [String] guest_name The guest that invoked firing the triggers # @param [String] guest_name The guest that invoked firing the triggers
def fire_before_triggers(action, guest_name) def fire_triggers(action, stage, guest_name)
# get all triggers matching action # get all triggers matching action
triggers = []
if stage == :before
triggers = config.before_triggers.select { |t| t.command == action } triggers = config.before_triggers.select { |t| t.command == action }
elsif stage == :after
triggers = config.after_triggers.select { |t| t.command == action }
else
# raise error, stage was not given
# This is an internal error
# TODO: Make sure this error exist
raise Errors::Triggers::NoStageGiven
end
triggers = filter_triggers(triggers, guest_name) triggers = filter_triggers(triggers, guest_name)
unless triggers.empty? unless triggers.empty?
@logger.info("Firing trigger for action #{action} on guest #{guest_name}") @logger.info("Firing trigger for action #{action} on guest #{guest_name}")
# TODO I18N me # TODO I18N me
@machine_ui.info("Running triggers before #{action}...") @machine_ui.info("Running triggers #{stage} #{action}...")
fire(triggers, guest_name)
end
end
# Fires all after triggers, if any are defined for the action and guest
#
# @param [Symbol] action Vagrant command to fire trigger on
# @param [String] guest_name The guest that invoked firing the triggers
def fire_after_triggers(action, guest_name)
# get all triggers matching action
triggers = config.after_triggers.select { |t| t.command == action }
triggers = filter_triggers(triggers, guest_name)
unless triggers.empty?
@logger.info("Firing triggers for action #{action} on guest #{guest_name}")
# TODO I18N me
@machine_ui.info("Running triggers after #{action}...")
fire(triggers, guest_name) fire(triggers, guest_name)
end end
end end