provisioners/shell: allow array args [GH-1949]

This commit is contained in:
Mitchell Hashimoto 2013-11-25 13:36:51 -08:00
parent 741930b439
commit 57d4775140
5 changed files with 27 additions and 10 deletions

View File

@ -43,6 +43,7 @@ IMPROVEMENTS:
- provisioners/ansible: allow files for extra vars [GH-2366]
- provisioners/shell: Added `keep_color` option to not automatically color
output based on stdout/stderr. [GH-2505]
- provisioners/shell: Arguments can now be an array of args. [GH-1949]
- synced\_folders/nfs: Specify `nfs_udp` to false to disable UDP based
NFS folders. [GH-2304]

View File

@ -56,8 +56,8 @@ module VagrantPlugins
end
# If there are args and its not a string, that is a problem
if args && !args.is_a?(String)
errors << I18n.t("vagrant.provisioners.shell.args_not_string")
if args && (!args.is_a?(String) || !args.is_a?(Array))
errors << I18n.t("vagrant.provisioners.shell.args_bad_type")
end
{ "shell provisioner" => errors }

View File

@ -7,11 +7,14 @@ module VagrantPlugins
module Shell
class Provisioner < Vagrant.plugin("2", :provisioner)
def provision
case config.args
when String then args = " #{config.args}"
when Array then args = " #{config.args.map{|arg| quote_and_escape(arg)}.join(" ")}"
else args = ""
args = ""
if args.is_a?(String)
args = " #{config.args}"
elsif args.is_a?(Array)
args = config.args.map { |a| quote_and_escape(a) }
args = " #{args.join(" ")}"
end
command = "chmod +x #{config.upload_path} && #{config.upload_path}#{args}"
with_script_file do |path|
@ -53,7 +56,7 @@ module VagrantPlugins
protected
# Quote and escape strings for shell execution, thanks to @capistrano
# Quote and escape strings for shell execution, thanks to Capistrano.
def quote_and_escape(text, quote = '"')
"#{quote}#{text.gsub(/#{quote}/) { |m| "#{m}\\#{m}#{m}" }}#{quote}"
end

View File

@ -1266,7 +1266,7 @@ en:
running_puppetd: "Running Puppet agent..."
shell:
args_not_string: "Shell provisioner `args` must be a string."
args_bad_type: "Shell provisioner `args` must be a string or array."
no_path_or_inline: "One of `path` or `inline` must be set."
path_and_inline_set: "Only one of `path` or `inline` may be set."
path_invalid: "`path` for shell provisioner does not exist on the host system: %{path}"

View File

@ -29,10 +29,11 @@ is required:
The remainder of the available options are optional:
* `args` (string) - Arguments to pass to the shell script when executing it
* `args` (string or array) - Arguments to pass to the shell script when executing it
as a single string. These arguments must be written as if they were typed
directly on the command line, so be sure to escape characters, quote,
etc. as needed.
etc. as needed. You may also pass the arguments in using an array. In this
case, Vagrant will handle quoting for you.
* `binary` (boolean) - Vagrant automatically replaces Windows line endings with
Unix line endings. If this is true, then Vagrant will not do this. By default
@ -131,3 +132,15 @@ Vagrant.configure("2") do |config|
end
end
```
You can also specify arguments an array if you don't want to worry about
quoting:
```ruby
Vagrant.configure("2") do |config|
config.vm.provision "shell" do |s|
s.inline = "echo $1"
s.args = ["hello, world!"]
end
end
```