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

Issue #411: Add config-clear command, fix bug in config-set. #412

Merged
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
56 changes: 51 additions & 5 deletions commands/config.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ function config_bee_command() {
'bee config-set image.style.thumbnail effects.0.data.width 200' => bt('Change the width of the Thumbnail image style.'),
),
),
'config-clear' => array(
'description' => bt('Clear the value of an option in a config file.'),
'callback' => 'config_clear_bee_callback',
'group' => 'configuration',
'arguments' => array(
'file' => bt("The name of the config object to retrieve. This is the name of the config file, less the '.json' suffix."),
'option' => bt('The name of the config option within the file to clear. This may contain periods to indicate levels within the config file.'),
),
'aliases' => array('cclr'),
'bootstrap' => BEE_BOOTSTRAP_FULL,
'examples' => array(
'bee config-clear somefile somevalue' => bt('Clear the option somevalue in the file somefile.json.'),
),
),
'config-import' => array(
'description' => bt('Import config into the site.'),
'callback' => 'config_import_bee_callback',
Expand Down Expand Up @@ -112,18 +126,19 @@ function config_set_bee_callback($arguments, $options) {
return;
}
}

// Check the existing value and type.
$existing_value = $config->get($arguments['option']);
$existing_type = gettype($existing_value);

// Disallow setting the value of complex data types (e.g. arrays).
elseif (!is_scalar($config->get($arguments['option']))) {
if (!is_scalar($existing_value) && $existing_type != 'NULL') {
bee_message(bt("'!option' is a complex data type and cannot be set via the command line.", array(
'!option' => $arguments['option'],
)), 'error');
return;
}

// Check the existing type.
$existing_value = $config->get($arguments['option']);
$existing_type = gettype($existing_value);

// Attempt to set the config item using the correct type.
$value = $arguments['value'];
$config_item = $arguments['option'];
Expand Down Expand Up @@ -199,6 +214,37 @@ function config_set_bee_callback($arguments, $options) {
)), 'success');
}

/**
* Command callback: Clear the value of a specific config option in a given
* file.
*/
function config_clear_bee_callback($arguments, $options) {
// Get the config value.
$value = config_get($arguments['file'], $arguments['option']);

// Display errors for blank/missing values.
if (!empty($arguments['option']) && is_null($value)) {
bee_message(bt("'!option' could not be found in the '!file' config file.", array(
'!option' => $arguments['option'],
'!file' => $arguments['file'],
)), 'error');
return;
}
elseif (empty($arguments['option']) && empty($value)) {
bee_message(bt("The '!file' config file could not be found.", array(
'!file' => $arguments['file'],
)), 'error');
return;
}

// Clear the value.
config_clear($arguments['file'], $arguments['option']);
bee_message(bt("'!option' has been cleared from '!file'.", array(
'!option' => $arguments['option'],
'!file' => $arguments['file'],
)), 'success');
}

/**
* Command callback: Import config into the site.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/backdrop/ConfigCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ public function test_config_set_command_works() {
exec("bee config-set $file $option2 $value2");
}

/**
* Make sure that the config-clear command works.
*/
public function test_config_clear_command_works() {
$file = 'system.core';
$option = 'foobar';
$new = 'bazvalue';

// Make sure nonexistent values can be set.
$output = shell_exec("bee config-set $file $option $new");
$this->assertStringContainsString("'$option' was set to '$new' in '$file'.", (string) $output);

// Make sure values can be cleared.
$output = shell_exec("bee config-clear $file $option");
$this->assertStringContainsString("'$option' has been cleared from '$file'.", (string) $output);
}

/**
* Make sure that the config-export command works.
*/
Expand Down
Loading