vagrant/plugins/commands/push/command.rb

81 lines
2.3 KiB
Ruby
Raw Normal View History

2014-10-23 17:51:18 +00:00
require 'optparse'
module VagrantPlugins
module CommandPush
class Command < Vagrant.plugin("2", :command)
def self.synopsis
"deploys code in this environment to a configured destination"
end
def execute
options = { all: false }
2014-10-23 17:51:18 +00:00
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant push [strategy] [options]"
o.on("-a", "--all", "Run all defined push strategies") do
options[:all] = true
end
2014-10-23 17:51:18 +00:00
end
# Parse the options
argv = parse_options(opts)
return if !argv
names = validate_pushes!(@env.pushes, argv, options)
2014-10-23 17:51:18 +00:00
names.each do |name|
@logger.debug("'push' environment with strategy: `#{name}'")
@env.push(name)
end
2014-10-23 17:51:18 +00:00
0
end
# Validate that the given list of names corresponds to valid pushes.
#
# @raise Vagrant::Errors::PushesNotDefined
# if there are no pushes defined
# @raise Vagrant::Errors::PushStrategyNotProvided
# if there are multiple push strategies defined and none were specified
# and `--all` was not given
# @raise Vagrant::Errors::PushStrategyNotDefined
# if any of the given push names do not correspond to a push strategy
2014-10-23 17:51:18 +00:00
#
# @param [Array<Symbol>] pushes
# the list of pushes defined by the environment
# @param [Array<String>] names
# the list of names provided by the user on the command line
# @param [Hash] options
# a list of options to pass to the validation
2014-10-23 17:51:18 +00:00
#
# @return [Array<Symbol>]
# the compiled list of pushes
2014-10-23 17:51:18 +00:00
#
def validate_pushes!(pushes, names = [], options = {})
2014-10-23 17:51:18 +00:00
if pushes.nil? || pushes.empty?
raise Vagrant::Errors::PushesNotDefined
end
names = Array(names).flatten.compact.map(&:to_sym)
if names.empty? || options[:all]
if options[:all] || pushes.length == 1
return pushes.map(&:to_sym)
else
2014-10-23 17:51:18 +00:00
raise Vagrant::Errors::PushStrategyNotProvided, pushes: pushes
end
end
names.each do |name|
if !pushes.include?(name)
2014-10-23 17:51:18 +00:00
raise Vagrant::Errors::PushStrategyNotDefined,
name: name,
pushes: pushes
end
end
return names
2014-10-23 17:51:18 +00:00
end
end
end
end