Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added #env-vars for adding environmental variables to Drush commands #955

Merged
merged 2 commits into from
Nov 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion includes/backend.inc
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ function _drush_backend_adjust_options($site_record, $command, $command_options,
* Optional. Defaults to the current user. If you specify this, you can choose which module to send.
* 'ssh-options'
* Optional. Defaults to "-o PasswordAuthentication=no"
* '#env-vars'
* Optional. An associative array of environmental variables to prefix the Drush command with.
* 'path-aliases'
* Optional; contains paths to folders and executables useful to the command.
* '%drush-script'
Expand Down Expand Up @@ -828,8 +830,9 @@ function drush_backend_invoke_concurrent($invocations, $common_options = array()
// that is running right now. For remote commands, we will run a wrapper
// script instead of drush.php -- drush.bat on Windows, or drush on Linux.
$drush_path = $site_record['path-aliases']['%drush-script'];
$env_vars = $site_record['#env-vars'];
$php = array_key_exists('php', $site_record) ? $site_record['php'] : (array_key_exists('php', $command_options) ? $command_options['php'] : NULL);
$drush_command_path = drush_build_drush_command($drush_path, $php, $os, array_key_exists('remote-host', $site_record));
$drush_command_path = drush_build_drush_command($drush_path, $php, $os, array_key_exists('remote-host', $site_record), $env_vars);
$cmd = _drush_backend_generate_command($site_record, $drush_command_path . " " . _drush_backend_argument_string($drush_global_options, $os) . " " . $site_record_to_dispatch . " " . $command, $args, $commandline_options, $backend_options) . ' 2>&1';
$cmds[$site] = array(
'cmd' => $cmd,
Expand Down Expand Up @@ -1046,11 +1049,13 @@ function drush_backend_generate_sitealias($backend_options) {
'remote-user' => NULL,
'ssh-options' => NULL,
'drush-script' => NULL,
'env-vars' => NULL
);
return array(
'remote-host' => $backend_options['remote-host'],
'remote-user' => $backend_options['remote-user'],
'ssh-options' => $backend_options['ssh-options'],
'#env-vars' => $backend_options['env-vars'],
'path-aliases' => array(
'%drush-script' => $backend_options['drush-script'],
),
Expand All @@ -1068,6 +1073,8 @@ function drush_backend_generate_sitealias($backend_options) {
* Optional. Defaults to the current user. If you specify this, you can choose which module to send.
* 'ssh-options'
* Optional. Defaults to "-o PasswordAuthentication=no"
* '#env-vars'
* Optional. An associative array of environmental variables to prefix the Drush command with.
* 'path-aliases'
* Optional; contains paths to folders and executables useful to the command.
* '%drush-script'
Expand Down
20 changes: 13 additions & 7 deletions includes/environment.inc
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,10 @@ function drush_verify_cli() {
* Build a drush command suitable for use for drush to call itself
* e.g. in backend_invoke.
*/
function drush_build_drush_command($drush_path = NULL, $php = NULL, $os = NULL, $remote_command = FALSE) {
$environment_variables = array();
function drush_build_drush_command($drush_path = NULL, $php = NULL, $os = NULL, $remote_command = FALSE, $environment_variables = array()) {
$os = _drush_get_os($os);
$additional_options = '';
$prefix = '';
if (!isset($drush_path)) {
if (!$remote_command) {
$drush_path = DRUSH_COMMAND;
Expand All @@ -465,7 +465,7 @@ function drush_build_drush_command($drush_path = NULL, $php = NULL, $os = NULL,
$additional_options .= ' --php=' . drush_escapeshellarg($php, $os);
}
// We will also add in the php options from --php-options
$prefix = drush_escapeshellarg($php, $os);
$prefix .= drush_escapeshellarg($php, $os);
$php_options = implode(' ', drush_get_context_options('php-options'));
if (!empty($php_options)) {
$prefix .= ' ' . $php_options;
Expand All @@ -476,16 +476,22 @@ function drush_build_drush_command($drush_path = NULL, $php = NULL, $os = NULL,
// Set environment variables to propogate config to redispatched calls.
if (drush_has_bash($os)) {
if ($php) {
$environment_variables[] = 'DRUSH_PHP=' . drush_escapeshellarg($php, $os);
$environment_variables['DRUSH_PHP'] = $php;
}
if ($php_options_alias = drush_get_option('php-options', NULL, 'alias')) {
$environment_variables[] = 'PHP_OPTIONS=' . drush_escapeshellarg($php_options_alias, $os);
$environment_variables['PHP_OPTIONS'] = $php_options_alias;
}
$columns = drush_get_context('DRUSH_COLUMNS');
if (($columns) && ($columns != 80)) {
$environment_variables[] = 'COLUMNS=' . $columns;
$environment_variables['COLUMNS'] = $columns;
}
$prefix = implode(' ', $environment_variables);
}
}

// Add environmental variables, if present
if (!empty($environment_variables)) {
foreach ($environment_variables as $key=>$value) {
$prefix .= ' ' . drush_escapeshellarg($key, $os) . '=' . drush_escapeshellarg($value, $os);
}
}

Expand Down