Skip to content

Commit bd83374

Browse files
committed
allow for array input for commands
1 parent 035859f commit bd83374

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

bolt-modules/boltlib/lib/puppet/functions/run_command.rb

+54-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'bolt/error'
44
require 'json'
5+
require 'shellwords'
56

67
# Runs a command on the given set of targets and returns the result from each command execution.
78
# This function does nothing if the list of targets is empty.
@@ -66,7 +67,7 @@ def run_command(command, targets, options = {})
6667
end
6768

6869
def run_commands(commands, targets, options = {})
69-
run_command_with_description(commands.join(' && '), targets, nil, options)
70+
run_command_with_description(commands, targets, nil, options)
7071
end
7172

7273
def run_command_with_description(command, targets, description = nil, options = {})
@@ -104,24 +105,60 @@ def run_command_with_description(command, targets, description = nil, options =
104105
# Ensure that given targets are all Target instances
105106
targets = inventory.get_targets(targets)
106107

107-
if targets.empty?
108-
call_function('debug', "Simulating run_command('#{command}') - no targets given - no action taken")
109-
Bolt::ResultSet.new([])
110-
else
111-
file_line = Puppet::Pops::PuppetStack.top_of_stack
112-
r = if executor.in_parallel?
113-
executor.run_in_thread do
114-
executor.run_command(targets, command, options, file_line)
115-
end
116-
else
117-
executor.run_command(targets, command, options, file_line)
108+
# if command is an array, run each command in sequence
109+
if command.is_a?(Array)
110+
results = []
111+
command.each do |cmd|
112+
# Split the command into an array of arguments
113+
split_cmd = Shellwords.split(cmd)
114+
115+
# Escape each argument
116+
escaped_cmd = split_cmd.map { |arg| Shellwords.escape(arg) }.join(' ')
117+
118+
if targets.empty?
119+
call_function('debug', "Simulating run_command('#{escaped_cmd}') - no targets given - no action taken")
120+
Bolt::ResultSet.new([])
121+
else
122+
file_line = Puppet::Pops::PuppetStack.top_of_stack
123+
result = if executor.in_parallel?
124+
executor.run_in_thread do
125+
executor.run_command(targets, escaped_cmd, options, file_line)
126+
end
127+
else
128+
executor.run_command(targets, escaped_cmd, options, file_line)
129+
end
130+
131+
if !result.ok && !options[:catch_errors]
132+
raise Bolt::RunFailure.new(result, 'run_command', escaped_cmd)
118133
end
119-
120-
if !r.ok && !options[:catch_errors]
121-
raise Bolt::RunFailure.new(r, 'run_command', command)
134+
results.concat(result.results)
135+
end
136+
end
137+
Bolt::ResultSet.new(results)
138+
else
139+
# Split the command into an array of arguments
140+
split_cmd = Shellwords.split(command)
141+
142+
escaped_cmd = split_cmd.map { |arg| Shellwords.escape(arg) }.join(' ')
143+
144+
if targets.empty?
145+
call_function('debug', "Simulating run_command('#{escaped_cmd}') - no targets given - no action taken")
146+
Bolt::Result.new([])
147+
else
148+
file_line = Puppet::Pops::PuppetStack.top_of_stack
149+
result = if executor.in_parallel?
150+
executor.run_in_thread do
151+
executor.run_command(targets, escaped_cmd, options, file_line)
152+
end
153+
else
154+
executor.run_command(targets, escaped_cmd, options, file_line)
155+
end
156+
157+
if !result.ok && !options[:catch_errors]
158+
raise Bolt::RunFailure.new(result, 'run_command', escaped_cmd)
159+
end
160+
result
122161
end
123-
124-
r
125162
end
126163
end
127164
end

0 commit comments

Comments
 (0)