Newer
Older
Moshe Weitzman
committed
* To run this *fun* command, execute `sudo drush --include=./examples mmas`
* from within your drush directory.
*
* See `drush topic docs-commands` for more information about command authoring.
Moshe Weitzman
committed
* You can copy this file to any of the following
* 1. A .drush folder in your HOME folder.
* 2. Anywhere in a folder tree below an active module on your site.
Moshe Weitzman
committed
* 3. /usr/share/drush/commands (configurable)
* 4. In an arbitrary folder specified with the --include option.
Moshe Weitzman
committed
* 5. Drupal's sites/all/drush folder.
*/
/**
* Implementation of hook_drush_command().
*
* In this hook, you specify which commands your
* drush module makes available, what it does and
* Notice how this structure closely resembles how
* See `drush topic docs-commands` for a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
Moshe Weitzman
committed
function sandwich_drush_command() {
// The 'make-me-a-sandwich' command
Moshe Weitzman
committed
$items['make-me-a-sandwich'] = array(
'description' => "Makes a delicious sandwich.",
'arguments' => array(
Moshe Weitzman
committed
'filling' => 'The type of the sandwich (turkey, cheese, etc.). Defaults to ascii.',
Moshe Weitzman
committed
),
'options' => array(
Moshe Weitzman
committed
'spreads' => array(
'description' => 'Comma delimited list of spreads.',
'example-value' => 'mayonnaise,mustard',
),
Moshe Weitzman
committed
),
'examples' => array(
Moshe Weitzman
committed
'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
Moshe Weitzman
committed
),
'aliases' => array('mmas'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
// Commandfiles may also add topics. These will appear in
// the list of topics when `drush topic` is executed.
// To view this topic, run `drush --include=/full/path/to/examples topic`
$items['sandwich-exposition'] = array(
Jonathan
committed
'description' => 'Ruminations on the true meaning and philosophy of sandwiches.',
'hidden' => TRUE,
'topic' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'callback' => 'drush_print_file',
'callback arguments' => array(dirname(__FILE__) . '/sandwich-topic.txt'),
);
Moshe Weitzman
committed
/**
* Implementation of hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help <name-of-your-command>'. This hook is optional. If a command
* does not implement this hook, the command's description is used instead.
* This hook is also used to look up help metadata, such as help
* category title and summary. See the comments below for a description.
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
Moshe Weitzman
committed
function sandwich_drush_help($section) {
Moshe Weitzman
committed
case 'drush:make-me-a-sandwich':
return dt("This command will make you a delicious sandwich, just how you like it.");
// The 'title' meta item is used to name a group of
// commands in `drush help`. If a title is not defined,
// the default is "All commands in ___", with the
// specific name of the commandfile (e.g. sandwich).
// Command files with less than four commands will
// be placed in the "Other commands" section, _unless_
// they define a title. It is therefore preferable
// to not define a title unless the file defines a lot
// of commands.
case 'meta:sandwich:title':
return dt("Sandwich commands");
// The 'summary' meta item is displayed in `drush help --filter`,
// and is used to give a general idea what the commands in this
// command file do, and what they have in common.
case 'meta:sandwich:summary':
return dt("Automates your sandwich-making business workflows.");
Moshe Weitzman
committed
}
}
Moshe Weitzman
committed
* Implementation of drush_hook_COMMAND_validate().
Greg Anderson
committed
*
* The validate command should exit with
* `return drush_set_error(...)` to stop execution of
* the command. In practice, calling drush_set_error
* OR returning FALSE is sufficient. See drush.api.php
* for more details.
Moshe Weitzman
committed
*/
function drush_sandwich_make_me_a_sandwich_validate() {
Greg Anderson
committed
if (drush_is_windows()) {
// $name = drush_get_username();
Greg Anderson
committed
// TODO: implement check for elevated process using w32api
// as sudo is not available for Windows
// http://php.net/manual/en/book.w32api.php
// http://social.msdn.microsoft.com/Forums/en/clr/thread/0957c58c-b30b-4972-a319-015df11b427d
}
else {
$name = posix_getpwuid(posix_geteuid());
if ($name['name'] !== 'root') {
return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
}
Moshe Weitzman
committed
* Example drush command callback. This is where the action takes place.
Moshe Weitzman
committed
* The function name should be same as command name but with dashes turned to
Moshe Weitzman
committed
* underscores and 'drush_commandfile_' prepended, where 'commandfile' is
Moshe Weitzman
committed
* taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'.
Moshe Weitzman
committed
* 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.
Moshe Weitzman
committed
* 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
Greg Anderson
committed
* with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute")
* if you want that all hook functions are still called (e.g.
* drush_example_pre_foo_execute, and so on).
Moshe Weitzman
committed
*
* In this function, all of Drupal's API is (usually) available, including
* any functions you have added in your own modules/themes.
*
Greg Anderson
committed
* @see drush_invoke()
* @see drush.api.php
Moshe Weitzman
committed
function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
$str_spreads = '';
// Read options with drush_get_option. Note that the options _must_
Greg Anderson
committed
// be documented in the $items structure for this command in the 'command' hook.
// See `drush topic docs-commands` for more information.
Moshe Weitzman
committed
if ($spreads = drush_get_option('spreads')) {
$list = implode(' and ', explode(',', $spreads));
$str_spreads = ' with just a dash of ' . $list;
}
$msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.',
array('!filling' => $filling, '!str_spreads' => $str_spreads)
);
drush_print("\n" . $msg . "\n");
Greg Anderson
committed
if (drush_get_context('DRUSH_NOCOLOR')) {
$filename = dirname(__FILE__) . '/sandwich-nocolor.txt';
Moshe Weitzman
committed
}
Greg Anderson
committed
else {
$filename = dirname(__FILE__) . '/sandwich.txt';
}
drush_print(file_get_contents($filename));
Moshe Weitzman
committed
/**
* Command argument complete callback. Provides argument
* values for shell completion.
*
* @return
* Array of popular fillings.
*/
function sandwich_make_me_a_sandwich_complete() {
return array('values' => array('turkey', 'cheese', 'jelly', 'butter'));
}