From c916b08ca13fb91267d29eaf1a5fc62958def82d Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 7 Jan 2019 19:56:14 +0000 Subject: Add support for environment variables in run_command --- lib/shell_utils.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/shell_utils.rb b/lib/shell_utils.rb index b22e791..22bf05b 100644 --- a/lib/shell_utils.rb +++ b/lib/shell_utils.rb @@ -23,7 +23,11 @@ require 'shellwords' require 'tempfile' module ShellUtils - def run_command(*command, run_remotely_on_host: nil) + def run_command( + *command, + run_remotely_on_host: nil, + environment_variables: {} + ) if run_remotely_on_host command = command.map do |arg| Shellwords.escape(arg) @@ -42,13 +46,21 @@ module ShellUtils '-o', 'StrictHostKeyChecking=no', '-i', identity_file.path, run_remotely_on_host.user_at_address, + *environment_variables_to_shell_arguments(environment_variables), *command ] + + local_environment_variables = {} + else + local_environment_variables = environment_variables end logger.info("#{self.class}: Running command #{command.join(' ')}") - Open3.popen2e(*command) do |_stdin, stdout_and_stderr, wait_thr| + Open3.popen2e( + local_environment_variables, + *command + ) do |_stdin, stdout_and_stderr, wait_thr| logger.info("#{self.class}: commmand running, pid #{wait_thr.pid}") output = [] @@ -119,4 +131,10 @@ module ShellUtils end end end + + def environment_variables_to_shell_arguments(environment_variables) + environment_variables.map do |key, value| + "#{key}=\"#{value}\"" + end + end end -- cgit v1.2.3