function drush_config_edit

8.0.x config.drush.inc drush_config_edit($config_name = '')
7.x config.drush.inc drush_config_edit($config_name = '')
master config.drush.inc drush_config_edit($config_name = '')

Edit command callback.

File

commands/core/config.drush.inc, line 696
Provides Configuration Management commands.

Code

function drush_config_edit($config_name = '') {
  if (empty($config_name) && $file = drush_get_option('file', FALSE)) {
    // If not provided, assume config name from the given file.
    $config_name = basename($file, '.yml');
  }

  // Identify and validate input.
  if ($config_name) {
    $config = Drupal::configFactory()->getEditable($config_name);
    if ($config->isNew()) {
      return drush_set_error(dt('Config !name does not exist', array('!name' => $config_name)));
    }
  }
  else {
    $config_names = \Drupal::configFactory()->listAll();
    $choice = drush_choice($config_names, 'Choose a configuration.');
    if (empty($choice)) {
      return drush_user_abort();
    }
    else {
      $config_name = $config_names[$choice];
      $config = Drupal::configFactory()->getEditable($config_name);
    }
  }

  $active_storage = $config->getStorage();
  $contents = $active_storage->read($config_name);

  $temp_storage = new FileStorage(drush_tempdir());
  if ($file) {
    $temp_storage->write($config_name, \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file)));
    // Show difference.
    $existing = new FileStorage(drush_tempdir());
    $existing->write($config_name, $contents);
    // @todo Can DiffFormatter produce a CLI pretty diff?
    drush_shell_exec('diff -u %s %s', $existing->getFilePath($config_name), $temp_storage->getFilePath($config_name));
    $output = drush_shell_exec_output();
    drush_print(implode("\n", $output));

    if (!drush_confirm(dt('Keep these changes?'))) {
      return drush_user_abort(dt('Config not edited.'));
    }
  }
  else {
    // Write tmp YAML file for editing.
    $temp_storage->write($config_name, $contents);

    // $filepath = drush_save_data_to_temp_file();
    $exec = drush_get_editor();
    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)) {
    $new_data = $temp_storage->read($config_name);
    $temp_storage->delete($config_name);
    $config->setData($new_data);
    $config->save();
  }
}