sandwich.drush.inc

  1. 8.0.x examples/sandwich.drush.inc
  2. 6.x examples/sandwich.drush.inc
  3. 7.x examples/sandwich.drush.inc
  4. 3.x examples/sandwich.drush.inc
  5. 4.x examples/sandwich.drush.inc
  6. 5.x examples/sandwich.drush.inc
  7. master 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.

Functions

Namesort descending Description
drush_sandwich_make_me_a_sandwich Example drush command callback. This is where the action takes place.
drush_sandwich_make_me_a_sandwich_validate Implementation of drush_hook_COMMAND_validate().
sandwich_drush_command Implementation of hook_drush_command().
sandwich_drush_help Implementation of hook_drush_help().

File

examples/sandwich.drush.inc
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. */
  17. /**
  18. * Implementation of hook_drush_command().
  19. *
  20. * In this hook, you specify which commands your
  21. * drush module makes available, what it does and
  22. * description.
  23. *
  24. * Notice how this structure closely resembles how
  25. * you define menu hooks.
  26. *
  27. * See `drush topic docs-commands` for a list of recognized keys.
  28. *
  29. * @return
  30. * An associative array describing your command(s).
  31. */
  32. function sandwich_drush_command() {
  33. $items = array();
  34. // The 'make-me-a-sandwich' command
  35. $items['make-me-a-sandwich'] = array(
  36. 'description' => "Makes a delicious sandwich.",
  37. 'arguments' => array(
  38. 'filling' => 'The type of the sandwich (turkey, cheese, etc.)',
  39. ),
  40. 'options' => array(
  41. 'spreads' => 'Comma delimited list of spreads (e.g. mayonnaise, mustard)',
  42. ),
  43. 'examples' => array(
  44. 'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
  45. ),
  46. 'aliases' => array('mmas'),
  47. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
  48. );
  49. // Commandfiles may also add topics. These will appear in
  50. // the list of topics when `drush topic` is executed.
  51. // To view this topic, run `drush --include=/full/path/to/examples topic`
  52. $items['sandwich-exposition'] = array(
  53. 'description' => 'Ruminations on the true meaning and philosophy of sandwiches.',
  54. 'hidden' => TRUE,
  55. 'topic' => TRUE,
  56. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  57. 'callback' => 'drush_print_file',
  58. 'callback arguments' => array(dirname(__FILE__) . '/sandwich-topic.txt'),
  59. );
  60. return $items;
  61. }
  62. /**
  63. * Implementation of hook_drush_help().
  64. *
  65. * This function is called whenever a drush user calls
  66. * 'drush help <name-of-your-command>'. This hook is optional. If a command
  67. * does not implement this hook, the command's description is used instead.
  68. *
  69. * This hook is also used to look up help metadata, such as help
  70. * category title and summary. See the comments below for a description.
  71. *
  72. * @param
  73. * A string with the help section (prepend with 'drush:')
  74. *
  75. * @return
  76. * A string with the help text for your command.
  77. */
  78. function sandwich_drush_help($section) {
  79. switch ($section) {
  80. case 'drush:make-me-a-sandwich':
  81. return dt("This command will make you a delicious sandwich, just how you like it.");
  82. // The 'title' meta item is used to name a group of
  83. // commands in `drush help`. If a title is not defined,
  84. // the default is "All commands in ___", with the
  85. // specific name of the commandfile (e.g. sandwich).
  86. // Command files with less than four commands will
  87. // be placed in the "Other commands" section, _unless_
  88. // they define a title. It is therefore preferable
  89. // to not define a title unless the file defines a lot
  90. // of commands.
  91. case 'meta:sandwich:title':
  92. return dt("Sandwich commands");
  93. // The 'summary' meta item is displayed in `drush help --filter`,
  94. // and is used to give a general idea what the commands in this
  95. // command file do, and what they have in common.
  96. case 'meta:sandwich:summary':
  97. return dt("Automates your sandwich-making business workflows.");
  98. }
  99. }
  100. /**
  101. * Implementation of drush_hook_COMMAND_validate().
  102. *
  103. * The validate command should exit with
  104. * `return drush_set_error(...)` to stop execution of
  105. * the command. In practice, calling drush_set_error
  106. * OR returning FALSE is sufficient. See drush.api.php
  107. * for more details.
  108. */
  109. function drush_sandwich_make_me_a_sandwich_validate() {
  110. $name = posix_getpwuid(posix_geteuid());
  111. if ($name['name'] !== 'root') {
  112. return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
  113. }
  114. }
  115. /**
  116. * Example drush command callback. This is where the action takes place.
  117. *
  118. * The function name should be same as command name but with dashes turned to
  119. * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
  120. * taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'.
  121. * Note also that a simplification step is also done in instances where
  122. * the commandfile name is the same as the beginning of the command name,
  123. * "drush_example_example_foo" is simplified to just "drush_example_foo".
  124. * To also implement a hook that is called before your command, implement
  125. * "drush_hook_pre_example_foo". For a list of all available hooks for a
  126. * given command, run drush in --debug mode.
  127. *
  128. * If for some reason you do not want your hook function to be named
  129. * after your command, you may define a 'callback' item in your command
  130. * object that specifies the exact name of the function that should be
  131. * called. However, the specified callback function must still begin
  132. * with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute")
  133. * if you want that all hook functions are still called (e.g.
  134. * drush_example_pre_foo_execute, and so on).
  135. *
  136. * In this function, all of Drupal's API is (usually) available, including
  137. * any functions you have added in your own modules/themes.
  138. *
  139. * @see drush_invoke()
  140. * @see drush.api.php
  141. */
  142. function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
  143. $str_spreads = '';
  144. if ($spreads = drush_get_option('spreads')) {
  145. $list = implode(' and ', explode(',', $spreads));
  146. $str_spreads = ' with just a dash of ' . $list;
  147. }
  148. $msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.',
  149. array('!filling' => $filling, '!str_spreads' => $str_spreads)
  150. );
  151. drush_print("\n" . $msg . "\n");
  152. drush_print(file_get_contents(dirname(__FILE__) . '/sandwich.txt'));
  153. }