diff options
author | Christopher Baines <mail@cbaines.net> | 2019-01-07 19:56:14 +0000 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2019-01-07 19:56:14 +0000 |
commit | c916b08ca13fb91267d29eaf1a5fc62958def82d (patch) | |
tree | d3df3a2c2d5f8ee8684d5ac2689e456d67abfadb | |
parent | 9f5998031761f5c8ffd877f2fa84456c4d0bc4ca (diff) | |
download | govuk-mini-environment-admin-c916b08ca13fb91267d29eaf1a5fc62958def82d.tar govuk-mini-environment-admin-c916b08ca13fb91267d29eaf1a5fc62958def82d.tar.gz |
Add support for environment variables in run_command
-rw-r--r-- | lib/shell_utils.rb | 22 |
1 files 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 |