vagrant/plugins/commands/push/command.rb

79 lines
2.2 KiB
Ruby
Raw Normal View History

# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
2014-10-23 13:51:18 -04: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
2014-12-02 13:46:22 -05:00
# @todo support multiple strategies if requested by the community
2014-10-23 13:51:18 -04:00
def execute
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant push [strategy] [options]"
end
# Parse the options
argv = parse_options(opts)
return if !argv
name = validate_pushes!(@env.pushes, argv[0])
2014-10-23 13:51:18 -04:00
# Validate the configuration
@env.machine(@env.machine_names.first, @env.default_provider).action_raw(
:config_validate,
Vagrant::Action::Builtin::ConfigValidate)
@logger.debug("'push' environment with strategy: `#{name}'")
@env.push(name)
2014-10-23 13:51:18 -04: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
# @raise Vagrant::Errors::PushStrategyNotDefined
# if the given push name do not correspond to a push strategy
2014-10-23 13:51:18 -04:00
#
# @param [Array<Symbol>] pushes
# the list of pushes defined by the environment
# @param [String] name
# the name provided by the user on the command line
2014-10-23 13:51:18 -04:00
#
# @return [Symbol]
# the compiled list of pushes
2014-10-23 13:51:18 -04:00
#
def validate_pushes!(pushes, name = nil)
2014-10-23 13:51:18 -04:00
if pushes.nil? || pushes.empty?
raise Vagrant::Errors::PushesNotDefined
end
if name.nil?
if pushes.length == 1
return pushes.first.to_sym
else
2014-12-15 01:27:40 -05:00
raise Vagrant::Errors::PushStrategyNotProvided,
pushes: pushes.map(&:to_s)
2014-10-23 13:51:18 -04:00
end
end
name = name.to_sym
if !pushes.include?(name)
raise Vagrant::Errors::PushStrategyNotDefined,
name: name.to_s,
pushes: pushes.map(&:to_s)
2014-10-23 13:51:18 -04:00
end
return name
2014-10-23 13:51:18 -04:00
end
end
end
end