Skip to content

Commit

Permalink
Fix drush-ops#28. config-import and config-edit commands updated for …
Browse files Browse the repository at this point in the history
…D8 changes.
  • Loading branch information
weitzman authored and mradcliffe committed Sep 12, 2013
1 parent 755193a commit 01a0726
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 44 deletions.
113 changes: 71 additions & 42 deletions commands/core/config.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

/**
* @file
* Provides CMI commands.
* Provides Configuration Management commands.
*/

use Drupal\Core\Config\Config;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\FileStorage;
use Symfony\Component\Yaml\Parser;

/**
* Implementation of hook_drush_help().
Expand Down Expand Up @@ -59,17 +60,21 @@ function config_drush_command() {
'key' => 'The config key, for example "page.front".',
'value' => 'The value to assign to the config key.'
),
'required-arguments' => 1,
'options' => array(
'format' => array(
'description' => 'Format to parse the object. Use "string" for string (default), and "yaml" for YAML.',
'example-value' => 'yaml',
'value' => 'required',
),
'stdin' => 'Get value from STDIN.',
// A convenient way to pass a multiline value within a backend request.
'value' => array(
'description' => 'The value to assign to the config key (if any).',
'hidden' => TRUE,
),
),
'examples' => array(
'drush config-set system.site page.front node' => 'Sets system.site:page.front to node.',
'drush config-set system.site page.front node' => 'Sets system.site:page.front to "node".',
),
'aliases' => array('cset'),
'core' => array('8+'),
Expand Down Expand Up @@ -136,7 +141,7 @@ function drush_config_list($prefix = '') {
$names = config_get_storage_names_with_prefix($prefix);

if (empty($names)) {
// Just incase there is no config.
// Just in case there is no config.
if (!$prefix) {
return drush_set_error(dt('No config storage names found.'));
}
Expand Down Expand Up @@ -176,10 +181,9 @@ function drush_config_get($config_name, $key = NULL) {
* The data to save to config.
*/
function drush_config_set($config_name, $key = NULL, $data = NULL) {
// Allow stdin to be use to set config keys.
if (!isset($key) && !drush_get_option('stdin')) {
return drush_set_error('DRUSH_CONFIG_ERROR', dt('No config key specified.'));
}
// This hidden option is a convenient way to pass a value without passing a key.
$data = drush_get_option('value', $data);

if (!isset($data) && !drush_get_option('stdin')) {
return drush_set_error('DRUSH_CONFIG_ERROR', dt('No config value specified.'));
}
Expand All @@ -200,11 +204,16 @@ function drush_config_set($config_name, $key = NULL, $data = NULL) {
// Now, we parse the value.
switch (drush_get_option('format', 'string')) {
case 'yaml':
$data = Yaml::parse($data);
break;
if (!class_exists('Symfony\Component\Yaml')) {
// For Drush PSR-0 and Composer information, see http://drupal.org/node/1316322.
$path = DRUSH_BASE_PATH . '/lib/Yaml-' . DRUSH_YAML_VERSION;
require_once "$path/Yaml.php";
}
$parser = new Parser();
$data = $parser->parse($data, TRUE);
}

if (is_array($data) && drush_confirm(dt('Do you want to update or set mulltiple keys on !name config.', array('!name' => $config_name)))) {
if (is_array($data) && drush_confirm(dt('Do you want to update or set multiple keys on !name config.', array('!name' => $config_name)))) {
foreach ($data as $key => $value) {
$config->set($key, $value);
}
Expand All @@ -228,19 +237,48 @@ function drush_config_set($config_name, $key = NULL, $data = NULL) {
}

/**
* Import config command callback.
* Command callback. Import from staging directory.
*/
function drush_config_import() {
// Retrieve a list of differences between the active and staged configuration.
$source_storage = drupal_container()->get('config.storage.staging');
$target_storage = drupal_container()->get('config.storage');
$config_changes = config_sync_get_changes($source_storage, $target_storage);
if ($config_changes === FALSE) {
$config_comparer = new StorageComparer(Drupal::service('config.storage.staging'), Drupal::service('config.storage'));
if (!$config_comparer->createChangelist()->hasChanges()) {
return drush_log(dt('There are no changes to import.'), 'ok');
}
_drush_print_config_changes_table($config_changes);
$change_list = $config_comparer->getChangelist();
_drush_print_config_changes_table($change_list);
if (drush_confirm(dt('Import the listed configuration changes?'))) {
return drush_op('config_import');
return drush_op('_drush_config_import', $config_comparer);
}
}

// Copied from submitForm() at /core/modules/config/lib/Drupal/config/Form/ConfigSync.php
function _drush_config_import(StorageComparer $storage_comparer) {
$config_importer = new ConfigImporter(
$storage_comparer,
Drupal::service('event_dispatcher'),
Drupal::service('config.factory'),
Drupal::entityManager(),
Drupal::lock()
);
if ($config_importer->alreadyImporting()) {
drush_log('Another request may be synchronizing configuration already.', 'warning');
}
else{
try {
$config_importer->import();
drupal_flush_all_caches();
drush_log('The configuration was imported successfully.', 'success');
}
catch (ConfigException $e) {
// Return a negative result for UI purposes. We do not differentiate
// between an actual synchronization error and a failed lock, because
// concurrent synchronizations are an edge-case happening only when
// multiple developers or site builders attempt to do it without
// coordinating.
watchdog_exception('config_import', $e);
return drush_set_error('config_import_fail', 'The import failed due to an error. Any errors have been logged.');
}
}
}

Expand All @@ -250,7 +288,7 @@ function drush_config_import() {
function drush_config_edit($config_name = '') {
// Identify and validate input.
if ($config_name) {
$config = config($config_name);
$config = Drupal::config($config_name);
if ($config->isNew()) {
return drush_set_error(dt('Config !name does not exist', array('!name' => $config_name)));
}
Expand All @@ -266,32 +304,23 @@ function drush_config_edit($config_name = '') {
}
}

$active_storage = drupal_container()->get('config.storage');
$staging_storage = drupal_container()->get('config.storage.staging');
$filepath = $staging_storage->getFilePath($config_name);
$staging_directory = config_get_config_directory(CONFIG_STAGING_DIRECTORY);

// Wipe staging directory before we write our work file.
drush_delete_dir_contents($staging_directory, TRUE);
$active_storage = $config->getStorage();
$contents = $active_storage->read($config_name);

// Copy from active storage to staging directory.
$staging_storage->write($config_name, $active_storage->read($config_name));
// Write tmp YAML file for editing.
$temp_storage = new FileStorage(drush_tempdir());
$temp_storage->write($config_name, $contents);

// $filepath = drush_save_data_to_temp_file();
$exec = drush_get_editor();
drush_shell_exec_interactive($exec, $filepath, $filepath);
drush_shell_exec_interactive($exec, $temp_storage->getFilePath($config_name));
// Perform import operation if user did not immediately exit editor.
if (!drush_get_option('bg', FALSE)) {
$result = config_import();
if ($result === NULL) {
drush_log('No changes to import.', 'ok');
}
elseif ($result === FALSE) {
drush_set_error('Error importing new configuration.');
}
$new_data = $temp_storage->read($config_name);
$temp_storage->delete($config_name);
$config->setData($new_data);
$config->save();
}

// Cleanup staging.
drush_delete_dir_contents($staging_directory, TRUE);
}

/* Helper functions */
Expand Down Expand Up @@ -372,7 +401,7 @@ function _drush_print_config_changes_table(array $config_changes) {
case 'delete':
$colour = $red;
break;
case 'change':
case 'update':
$colour = $yellow;
break;
case 'create':
Expand Down
2 changes: 0 additions & 2 deletions commands/core/outputformat/yaml.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

define('DRUSH_YAML_VERSION', '2.2.1');

if (!class_exists('Symfony\Component\Yaml\Dumper')) {
// For Drush PSR-0 and Composer information, see http://drupal.org/node/1316322.
$path = DRUSH_BASE_PATH . '/lib/Yaml-' . DRUSH_YAML_VERSION;
Expand Down
5 changes: 5 additions & 0 deletions includes/environment.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ define('DRUSH_TABLE_VERSION', '1.1.3');
*/
define('DRUSH_PEAR_BASE_URL', 'http://download.pear.php.net/package/');

/**
* Used by outputformat engine and config-set in core.
*/
define('DRUSH_YAML_VERSION', '2.2.1');

/**
* Log PHP errors to the Drush log. This is in effect until Drupal's error
* handler takes over.
Expand Down

0 comments on commit 01a0726

Please sign in to comment.