Infer push name when only one strategy is defined, support multiple strategies

This commit is contained in:
Seth Vargo 2014-12-02 13:30:16 -05:00
parent 35dbb8db7d
commit 6b48199346
4 changed files with 98 additions and 34 deletions

View File

@ -571,7 +571,7 @@ module Vagrant
# #
# @return [Array<Symbol>] # @return [Array<Symbol>]
def pushes def pushes
vagrantfile.config.push.__compiled_pushes.keys self.vagrantfile.config.push.__compiled_pushes.keys
end end
# This returns a machine with the proper provider for this environment. # This returns a machine with the proper provider for this environment.

View File

@ -7,56 +7,73 @@ module VagrantPlugins
"deploys code in this environment to a configured destination" "deploys code in this environment to a configured destination"
end end
# @todo support multiple strategies if requested by the community
def execute def execute
options = { all: false }
opts = OptionParser.new do |o| opts = OptionParser.new do |o|
o.banner = "Usage: vagrant push [strategy] [options]" o.banner = "Usage: vagrant push [strategy] [options]"
o.on("-a", "--all", "Run all defined push strategies") do
options[:all] = true
end
end end
# Parse the options # Parse the options
argv = parse_options(opts) argv = parse_options(opts)
return if !argv return if !argv
name = argv[0] names = validate_pushes!(@env.pushes, argv, options)
pushes = @env.pushes
validate_pushes!(pushes, name) names.each do |name|
@logger.debug("'push' environment with strategy: `#{name}'")
@logger.debug("'push' environment with strategy: `#{name}'") @env.push(name)
@env.push(name) end
0 0
end end
# Validate that the given list of pushes and strategy are valid. # Validate that the given list of names corresponds to valid pushes.
# #
# @raise [PushesNotDefined] if there are no pushes defined for the # @raise Vagrant::Errors::PushesNotDefined
# environment # if there are no pushes defined
# @raise [PushStrategyNotDefined] if a strategy is given, but does not # @raise Vagrant::Errors::PushStrategyNotProvided
# correspond to one that exists in the environment # 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
# #
# @param [Registry] pushes The list of pushes as a {Registry} # @param [Array<Symbol>] pushes
# @param [#to_sym, nil] name The name of the strategy # 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
# #
# @return [true] # @return [Array<Symbol>]
def validate_pushes!(pushes, name=nil) # the compiled list of pushes
#
def validate_pushes!(pushes, names = [], options = {})
if pushes.nil? || pushes.empty? if pushes.nil? || pushes.empty?
raise Vagrant::Errors::PushesNotDefined raise Vagrant::Errors::PushesNotDefined
end end
if name.nil? names = Array(names).flatten.compact.map(&:to_sym)
if pushes.length != 1
if names.empty? || options[:all]
if options[:all] || pushes.length == 1
return pushes.map(&:to_sym)
else
raise Vagrant::Errors::PushStrategyNotProvided, pushes: pushes raise Vagrant::Errors::PushStrategyNotProvided, pushes: pushes
end end
else end
if !pushes.include?(name.to_sym)
names.each do |name|
if !pushes.include?(name)
raise Vagrant::Errors::PushStrategyNotDefined, raise Vagrant::Errors::PushStrategyNotDefined,
name: name, name: name,
pushes: pushes pushes: pushes
end end
end end
true return names
end end
end end
end end

View File

@ -25,15 +25,35 @@ describe VagrantPlugins::CommandPush::Command do
describe "#execute" do describe "#execute" do
before do before do
allow(subject).to receive(:validate_pushes!) allow(subject).to receive(:validate_pushes!)
.and_return([:noop])
allow(env).to receive(:pushes) allow(env).to receive(:pushes)
allow(env).to receive(:push) allow(env).to receive(:push)
end end
it "validates the pushes" do it "validates the pushes" do
expect(subject).to receive(:validate_pushes!).once expect(subject).to receive(:validate_pushes!)
.with(nil, argv, kind_of(Hash))
.once
subject.execute subject.execute
end end
it "parses arguments" do
list = ["noop", "ftp"]
instance = described_class.new(list, env)
expect(instance).to receive(:validate_pushes!)
.with(nil, list, kind_of(Hash))
.and_return([])
instance.execute
end
it "parses options" do
instance = described_class.new(["--all"], env)
expect(instance).to receive(:validate_pushes!)
.with(nil, argv, all: true)
.and_return([])
instance.execute
end
it "delegates to Environment#push" do it "delegates to Environment#push" do
expect(env).to receive(:push).once expect(env).to receive(:push).once
subject.execute subject.execute
@ -72,17 +92,22 @@ describe VagrantPlugins::CommandPush::Command do
end end
context "when that strategy is defined" do context "when that strategy is defined" do
it "does not raise an exception" do it "returns that push" do
expect { subject.validate_pushes!(pushes, :noop) } expect(subject.validate_pushes!(pushes, :noop)).to eq([:noop])
.to_not raise_error
end end
end end
end end
context "when no strategy is given" do context "when no strategy is given" do
it "does not raise an exception" do it "returns the push" do
expect { subject.validate_pushes!(pushes) } expect(subject.validate_pushes!(pushes)).to eq([:noop])
.to_not raise_error end
end
context "when --all is given" do
it "returns the push" do
expect(subject.validate_pushes!(pushes, [], all: true))
.to eq([:noop])
end end
end end
end end
@ -101,21 +126,33 @@ describe VagrantPlugins::CommandPush::Command do
end end
context "when that strategy is defined" do context "when that strategy is defined" do
it "does not raise an exception" do it "returns the strategy" do
expect { subject.validate_pushes!(pushes, :noop) } expect(subject.validate_pushes!(pushes, :noop)).to eq([:noop])
.to_not raise_error expect(subject.validate_pushes!(pushes, :ftp)).to eq([:ftp])
expect { subject.validate_pushes!(pushes, :ftp) }
.to_not raise_error
end end
end end
end end
context "when multiple strategies are given" do
it "returns the pushes" do
expect(subject.validate_pushes!(pushes, [:noop, :ftp]))
.to eq([:noop, :ftp])
end
end
context "when no strategy is given" do context "when no strategy is given" do
it "raises an exception" do it "raises an exception" do
expect { subject.validate_pushes!(pushes) } expect { subject.validate_pushes!(pushes) }
.to raise_error(Vagrant::Errors::PushStrategyNotProvided) .to raise_error(Vagrant::Errors::PushStrategyNotProvided)
end end
end end
context "when --all is given" do
it "returns the pushes" do
expect(subject.validate_pushes!(pushes, [], all: true))
.to eq([:noop, :ftp])
end
end
end end
end end
end end

View File

@ -52,6 +52,16 @@ subcommand:
$ vagrant push staging $ vagrant push staging
``` ```
Pushes will be run in the order you specify on the command line, **not the order
they are specified in the `Vagrantfile`!**
To execute all the Vagrant Push strategies, specify the `--all` flag with no
other arguments:
```shell
$ vagrant push --all
```
Vagrant Push is the easiest way to deploy your application. You can read more Vagrant Push is the easiest way to deploy your application. You can read more
in the documentation links on the sidebar. in the documentation links on the sidebar.