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.

Shows how to make your own drush command.

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
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. * Shows how to make your own drush command.
  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_parse_command() 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. $items['make-me-a-sandwich'] = array(
  35. 'description' => "Makes a delicious sandwich.",
  36. 'arguments' => array(
  37. 'filling' => 'The type of the sandwich (turkey, cheese, etc.)',
  38. ),
  39. 'options' => array(
  40. 'spreads' => 'Comma delimited list of spreads (e.g. mayonnaise, mustard)',
  41. ),
  42. 'examples' => array(
  43. 'drush make-me-a-sandwich turkey --spreads=ketchup,mustard',
  44. ),
  45. 'aliases' => array('mmas'),
  46. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
  47. );
  48. return $items;
  49. }
  50. /**
  51. * Implementation of hook_drush_help().
  52. *
  53. * This function is called whenever a drush user calls
  54. * 'drush help <name-of-your-command>'
  55. *
  56. * @param
  57. * A string with the help section (prepend with 'drush:')
  58. *
  59. * @return
  60. * A string with the help text for your command.
  61. */
  62. function sandwich_drush_help($section) {
  63. switch ($section) {
  64. case 'drush:make-me-a-sandwich':
  65. return dt("This command will make you a delicious sandwich, just how you like it.");
  66. }
  67. }
  68. /*
  69. * Implementation of drush_hook_COMMAND_validate().
  70. */
  71. function drush_sandwich_make_me_a_sandwich_validate() {
  72. $name = posix_getpwuid(posix_geteuid());
  73. if ($name['name'] !== 'root') {
  74. return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
  75. }
  76. }
  77. /**
  78. * Example drush command callback. This is where the action takes place.
  79. *
  80. * The function name should be same as command name but with dashes turned to
  81. * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
  82. * taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'.
  83. * Note also that a simplification step is also done in instances where
  84. * the commandfile name is the same as the beginning of the command name,
  85. * "drush_example_example_foo" is simplified to just "drush_example_foo".
  86. * To also implement a hook that is called before your command, implement
  87. * "drush_hook_pre_example_foo". For a list of all available hooks for a
  88. * given command, run drush in --debug mode.
  89. *
  90. * If for some reason you do not want your hook function to be named
  91. * after your command, you may define a 'callback' item in your command
  92. * object that specifies the exact name of the function that should be
  93. * called. However, the specified callback function must still begin
  94. * with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute").
  95. * All hook functions are still called (e.g. drush_example_pre_foo_execute,
  96. * and so on.)
  97. *
  98. * In this function, all of Drupal's API is (usually) available, including
  99. * any functions you have added in your own modules/themes.
  100. *
  101. */
  102. function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
  103. $str_spreads = '';
  104. if ($spreads = drush_get_option('spreads')) {
  105. $list = implode(' and ', explode(',', $spreads));
  106. $str_spreads = ' with just a dash of ' . $list;
  107. }
  108. $msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.',
  109. array('!filling' => $filling, '!str_spreads' => $str_spreads)
  110. );
  111. drush_print("\n" . $msg . "\n");
  112. drush_print(file_get_contents(dirname(__FILE__) . '/sandwich.txt'));
  113. }