Skip to content

Commit

Permalink
Merge pull request drush-ops#1158 from greg-1-anderson/499-vget-single
Browse files Browse the repository at this point in the history
drush-ops#499: Maintain Drush 5 compatible output from vget
  • Loading branch information
greg-1-anderson committed Feb 10, 2015
2 parents cdc7ffd + 5649bc0 commit 7875ba9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
31 changes: 22 additions & 9 deletions commands/core/variable.drush.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\Yaml\Yaml;

/**
* Implementation of hook_drush_command().
*
Expand All @@ -23,13 +25,14 @@ function variable_drush_command() {
'examples' => array(
'drush vget' => 'List all variables and values.',
'drush vget user' => 'List all variables containing the string "user".',
'drush vget site_mail --exact' => 'Show the variable with the exact key "site_mail".',
'drush vget site_mail --exact' => 'Show only the value of the variable with the exact key "site_mail".',
'drush vget site_mail --exact --pipe' => 'Show only the variable with the exact key "site_mail" without changing the structure of the output.',
),
'options' => array(
'exact' => 'Only get the one variable that exactly matches the specified name.',
'exact' => "Only get the one variable that exactly matches the specified name. Output will contain only the variable's value.",
),
'outputformat' => array(
'default' => 'labeled-export',
'default' => 'yaml',
'pipe-format' => 'config',
'variable-name' => 'variables',
'table-metadata' => array(
Expand Down Expand Up @@ -125,22 +128,28 @@ function variable_complete_variables() {
*/
function drush_variable_get() {
global $conf;
$exact = drush_get_option('exact', FALSE);

$keys = array_keys($conf);
if ($args = func_get_args()) {
$args[0] = drush_variable_name_adjust($args[0]);
if (drush_get_option('exact', FALSE)) {
if ($exact) {
$keys = in_array($args[0], $keys) ? array($args[0]) : array();
}
$keys = preg_grep("/{$args[0]}/", $keys);
}

$pipe = array();
foreach ($keys as $name) {
$value = $conf[$name];
$returns[$name] = $value;
// In --exact mode, if --pipe is not set, then simplify the return type.
if ($exact && !drush_get_context('DRUSH_PIPE')) {
$key = reset($keys);
$returns = isset($conf[$key]) ? $conf[$key] : FALSE;
}
else {
foreach ($keys as $name) {
$value = $conf[$name];
$returns[$name] = $value;
}
}

if (empty($keys)) {
return drush_set_error('No matching variable found.');
}
Expand Down Expand Up @@ -250,6 +259,10 @@ function _drush_variable_format($value, $format) {
case 'json':
$value = drush_json_decode($value);
break;

case 'yaml':
$value = Yaml::parse($value);
break;
}
return $value;
}
Expand Down
10 changes: 8 additions & 2 deletions tests/variableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ function testVariable() {
}

$sites = $this->setUpDrupal(1, TRUE);
$options = array(
$options_without_pipe = array(
'yes' => NULL,
'pipe' => NULL,
'root' => $this->webroot(),
'uri' => key($sites),
);
$options = $options_without_pipe + array(
'pipe' => NULL,
);

$this->drush('variable-set', array('test_integer', '3.14159'), $options);
$this->drush('variable-get', array('test_integer'), $options);
Expand All @@ -45,6 +47,10 @@ function testVariable() {
eval($var_export);
$this->assertEquals('exactish', $variables['site_n'], '--exact option works as expected.');

$this->drush('variable-get', array('site_n'), $options_without_pipe + array('exact' => NULL));
$site_n_value = $this->getOutput();
$this->assertEquals('exactish', $site_n_value, '--exact option works as expected with --pipe.');

$this->drush('variable-delete', array('site_name'), $options);
$output = $this->getOutput();
$this->assertEmpty($output, 'Variable was successfully deleted.');
Expand Down

0 comments on commit 7875ba9

Please sign in to comment.