sandwich.drush.inc

Tracking 7.x-5.x branch
  1. drupal
    1. 6 contributions/drush/examples/sandwich.drush.inc
    2. 7 contributions/drush/examples/sandwich.drush.inc

Example drush command.

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.

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. 3. /usr/share/drush/commands (configurable) 4. In an arbitrary folder specified with the --include option. 5. Drupal's sites/all/drush folder.

Functions & methods

NameDescription
drush_sandwich_make_me_a_sandwichExample drush command callback. This is where the action takes place.
drush_sandwich_make_me_a_sandwich_validateImplementation of drush_hook_COMMAND_validate().
sandwich_drush_commandImplementation of hook_drush_command().
sandwich_drush_helpImplementation of hook_drush_help().
sandwich_make_me_a_sandwich_completeCommand argument complete callback. Provides argument values for shell completion.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Example drush command.
  5. *
  6. * To run this *fun* command, execute `sudo drush --include=./examples mmas`
  7. * from within your drush directory.
  8. *
  9. * See `drush topic docs-commands` for more information about command authoring.
  10. *
  11. * You can copy this file to any of the following
  12. * 1. A .drush folder in your HOME folder.
  13. * 2. Anywhere in a folder tree below an active module on your site.
  14. * 3. /usr/share/drush/commands (configurable)
  15. * 4. In an arbitrary folder specified with the --include option.
  16. * 5. Drupal's sites/all/drush folder.
  17. */
  18. /**
  19. * Implementation of hook_drush_command().
  20. *
  21. * In this hook, you specify which commands your
  22. * drush module makes available, what it does and
  23. * description.
  24. *
  25. * Notice how this structure closely resembles how
  26. * you define menu hooks.
  27. *
  28. * See `drush topic docs-commands` for a list of recognized keys.
  29. *
  30. * @return
  31. * An associative array describing your command(s).
  32. */
  33. function sandwich_drush_command() {
  34. $items = array();
  35. // The 'make-me-a-sandwich' command
  36. $items['make-me-a-sandwich'] = array(
  37. 'description' => "Makes a delicious sandwich.",
  38. 'arguments' => array(
  39. 'filling' => 'The type of the sandwich (turkey, cheese, etc.). Defaults to ascii.',
  40. ),
  41. 'options' => array(
  42. 'spreads' => array(
  43. 'description' => 'Comma delimited list of spreads.',
  44. 'example-value' => 'mayonnaise,mustard',
  45. ),
  46. ),
  47. 'examples' => array(
  48. 'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
  49. ),
  50. 'aliases' => array('mmas'),
  51. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
  52. );
  53. // Commandfiles may also add topics. These will appear in
  54. // the list of topics when `drush topic` is executed.
  55. // To view this topic, run `drush --include=/full/path/to/examples topic`
  56. $items['sandwich-exposition'] = array(
  57. 'description' => 'Ruminations on the true meaning and philosophy of sandwiches.',
  58. 'hidden' => TRUE,
  59. 'topic' => TRUE,
  60. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  61. 'callback' => 'drush_print_file',
  62. 'callback arguments' => array(dirname(__FILE__) . '/sandwich-topic.txt'),
  63. );
  64. return $items;
  65. }
  66. /**
  67. * Implementation of hook_drush_help().
  68. *
  69. * This function is called whenever a drush user calls
  70. * 'drush help <name-of-your-command>'. This hook is optional. If a command
  71. * does not implement this hook, the command's description is used instead.
  72. *
  73. * This hook is also used to look up help metadata, such as help
  74. * category title and summary. See the comments below for a description.
  75. *
  76. * @param
  77. * A string with the help section (prepend with 'drush:')
  78. *
  79. * @return
  80. * A string with the help text for your command.
  81. */
  82. function sandwich_drush_help($section) {
  83. switch ($section) {
  84. case 'drush:make-me-a-sandwich':
  85. return dt("This command will make you a delicious sandwich, just how you like it.");
  86. // The 'title' meta item is used to name a group of
  87. // commands in `drush help`. If a title is not defined,
  88. // the default is "All commands in ___", with the
  89. // specific name of the commandfile (e.g. sandwich).
  90. // Command files with less than four commands will
  91. // be placed in the "Other commands" section, _unless_
  92. // they define a title. It is therefore preferable
  93. // to not define a title unless the file defines a lot
  94. // of commands.
  95. case 'meta:sandwich:title':
  96. return dt("Sandwich commands");
  97. // The 'summary' meta item is displayed in `drush help --filter`,
  98. // and is used to give a general idea what the commands in this
  99. // command file do, and what they have in common.
  100. case 'meta:sandwich:summary':
  101. return dt("Automates your sandwich-making business workflows.");
  102. }
  103. }
  104. /**
  105. * Implementation of drush_hook_COMMAND_validate().
  106. *
  107. * The validate command should exit with
  108. * `return drush_set_error(...)` to stop execution of
  109. * the command. In practice, calling drush_set_error
  110. * OR returning FALSE is sufficient. See drush.api.php
  111. * for more details.
  112. */
  113. function drush_sandwich_make_me_a_sandwich_validate() {
  114. if (drush_is_windows()) {
  115. // $name = getenv('USERNAME');
  116. // TODO: implement check for elevated process using w32api
  117. // as sudo is not available for Windows
  118. // http://php.net/manual/en/book.w32api.php
  119. // http://social.msdn.microsoft.com/Forums/en/clr/thread/0957c58c-b30b-4972-a319-015df11b427d
  120. }
  121. else {
  122. $name = posix_getpwuid(posix_geteuid());
  123. if ($name['name'] !== 'root') {
  124. return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
  125. }
  126. }
  127. }
  128. /**
  129. * Example drush command callback. This is where the action takes place.
  130. *
  131. * The function name should be same as command name but with dashes turned to
  132. * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
  133. * taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'.
  134. * Note also that a simplification step is also done in instances where
  135. * the commandfile name is the same as the beginning of the command name,
  136. * "drush_example_example_foo" is simplified to just "drush_example_foo".
  137. * To also implement a hook that is called before your command, implement
  138. * "drush_hook_pre_example_foo". For a list of all available hooks for a
  139. * given command, run drush in --debug mode.
  140. *
  141. * If for some reason you do not want your hook function to be named
  142. * after your command, you may define a 'callback' item in your command
  143. * object that specifies the exact name of the function that should be
  144. * called. However, the specified callback function must still begin
  145. * with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute")
  146. * if you want that all hook functions are still called (e.g.
  147. * drush_example_pre_foo_execute, and so on).
  148. *
  149. * In this function, all of Drupal's API is (usually) available, including
  150. * any functions you have added in your own modules/themes.
  151. *
  152. * @see drush_invoke()
  153. * @see drush.api.php
  154. */
  155. function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
  156. $str_spreads = '';
  157. // Read options with drush_get_option. Note that the options _must_
  158. // be documented in the $items structure for this command in the 'command' hook.
  159. // See `drush topic docs-commands` for more information.
  160. if ($spreads = drush_get_option('spreads')) {
  161. $list = implode(' and ', explode(',', $spreads));
  162. $str_spreads = ' with just a dash of ' . $list;
  163. }
  164. $msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.',
  165. array('!filling' => $filling, '!str_spreads' => $str_spreads)
  166. );
  167. drush_print("\n" . $msg . "\n");
  168. if (drush_get_context('DRUSH_NOCOLOR')) {
  169. $filename = dirname(__FILE__) . '/sandwich-nocolor.txt';
  170. }
  171. else {
  172. $filename = dirname(__FILE__) . '/sandwich.txt';
  173. }
  174. drush_print(file_get_contents($filename));
  175. }
  176. /**
  177. * Command argument complete callback. Provides argument
  178. * values for shell completion.
  179. *
  180. * @return
  181. * Array of popular fillings.
  182. */
  183. function sandwich_make_me_a_sandwich_complete() {
  184. return array('values' => array('turkey', 'cheese', 'jelly', 'butter'));
  185. }