function drush_variable_set

8.0.x variable.drush.inc drush_variable_set()
6.x variable.drush.inc drush_variable_set()
7.x variable.drush.inc drush_variable_set()
3.x variable.drush.inc drush_variable_set()
4.x variable.drush.inc drush_variable_set()
5.x variable.drush.inc drush_variable_set()
master variable.drush.inc drush_variable_set()

Command callback. Set a variable.

File

commands/core/variable.drush.inc, line 166

Code

function drush_variable_set() {
  $args = func_get_args();
  $value = $args[1];
  if (!isset($value)) {
    return drush_set_error('DRUSH_VARIABLE_ERROR', dt('No value specified.'));
  }

  $args[0] = drush_variable_name_adjust($args[0]);
  $result = drush_variable_like($args[0]);

  $options[] = "$args[0] " . dt('(new variable)');
  $match = FALSE;
  while (!$match && $name = drush_db_result($result)) {
    if ($name == $args[0]) {
      $options[0] = $name;
      $match = TRUE;
    }
    else {
      $options[] = $name;
    }
  }

  if ($value == '-') {
    $value = stream_get_contents(STDIN);
  }

  // If the value is a string (usual case, unless we are called from code),
  // then format the input
  if (is_string($value)) {
    $value = _drush_variable_format($value, drush_get_option('format', 'auto'));
  }

  // Format the output for display
  if (is_array($value)) {
    $display = "\n" . var_export($value, TRUE);
  }
  elseif (is_integer($value)) {
    $display = $value;
  }
  elseif (is_bool($value)) {
    $display = $value ? "TRUE" : "FALSE";
  }
  else {
    $display = '"' . $value . '"';
  }

  // Check 'always-set' for compatibility with older scripts; --exact is preferred.
  $always_set = drush_get_option('always-set', FALSE) || drush_get_option('exact', FALSE);

  if ($always_set || count($options) == 1 || $match) {
    variable_set($args[0], $value);
    drush_log(dt('!name was set to !value.', array('!name' => $args[0], '!value' => $display)), LogLevel::SUCCESS);
    return '';
  }
  else {
    $choice = drush_choice($options, 'Enter a number to choose which variable to set.');
    if ($choice === FALSE) {
      return drush_user_abort();
    }
    $choice = $options[$choice];
    $choice = str_replace(' ' . dt('(new variable)'), '', $choice);
    drush_op('variable_set', $choice, $value);
    drush_log(dt('!name was set to !value', array('!name' => $choice, '!value' => $display)), LogLevel::SUCCESS);
  }
}