Skip to content

Instantly share code, notes, and snippets.

@chrisfromredfin
Created February 12, 2014 20:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisfromredfin/8963894 to your computer and use it in GitHub Desktop.
Save chrisfromredfin/8963894 to your computer and use it in GitHub Desktop.
<?php
/**
* Implementation of hook_drush_command().
*
* In this hook, you specify which commands your
* drush module makes available, what it does and
* description.
*
* Notice how this structure closely resembles how
* you define menu hooks.
*
* @See drush_parse_command() for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function redfin_drush_command() {
$items = array();
$items['redfin-db'] = array(
'description' => 'Prints the db connection string from settings.php',
'arguments' => array(),
'examples' => array('drush redfin-db'),
'aliases' => array('rdb'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
);
$items['redfin-vr'] = array(
'callback' => 'redfin_revert_views',
'drupal dependencies' => array('views'),
'description' => 'Revert overridden views to their default state. Make sure to backup first.',
'arguments' => array(),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'aliases' => array('rvr'),
'examples' => array(
'drush rvr archive' => 'Reverts all yo views!',
),
);
return $items;
}
/**
* Implementation of hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help <name-of-your-command>'
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
function redfin_drush_help($section) {
switch ($section) {
case 'drush:redfin-db':
return dt("This command simply prints the DB connection string from settings.php");
break;
case 'drush:redfin-vr':
return dt("This command reverts ALL overridden views!");
break;
}
}
/**
* Example drush command callback. This is where the action takes place.
*
* The function name should be same as command name but with dashes turned to
* underscores and 'drush_commandfile_' prepended, where 'commandfile' is
* taken from the file 'commandfile.drush.inc', which in this case is 'redfin'.
* Note also that a simplification step is also done in instances where
* the commandfile name is the same as the beginning of the command name,
* "drush_example_example_foo" is simplified to just "drush_example_foo".
* To also implement a hook that is called before your command, implement
* "drush_hook_pre_example_foo". For a list of all available hooks for a
* given command, run drush in --debug mode.
*
* If for some reason you do not want your hook function to be named
* after your command, you may define a 'callback' item in your command
* object that specifies the exact name of the function that should be
* called. However, the specified callback function must still begin
* with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute").
* All hook functions are still called (e.g. drush_example_pre_foo_execute,
* and so on.)
*
* In this function, all of Drupal's API is (usually) available, including
* any functions you have added in your own modules/themes.
*
*/
function drush_redfin_db() {
if( drush_drupal_version() >= 7) {
global $databases;
$defdb = $databases['default']['default'];
print $defdb['driver'].'://'.$defdb['username'].':'.$defdb['password'].'@'.$defdb['host'].'/'.$defdb['database']."\n";
}
else {
global $db_url;
print $db_url."\n";
}
}
function redfin_revert_views() {
$i = views_revert_allviews(views_get_all_views());
drush_log(dt('Reverted a total of @count views.', array('@count' => $i)), 'ok');
}
@barraponto
Copy link

drush rvr should be made a shell alias to drush ctools-export-revert views_view. Just add to drushrc.php;

$options['shell-aliases']['rvr'] = 'drush ctools-export-revert views_view';

It should be one less command to maintain, and it lets you choose a specific view to revert, like drush rvr archive only reverting the default archive view that comes with views. See http://www.drushcommands.com/drush-6x/ctools/ctools-export-revert for more on that command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment