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. 5. Drupal's /drush or /sites/all/drush folders, or in the /drush folder in the directory above the Drupal root.

Functions

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. * 5. Drupal's /drush or /sites/all/drush folders, or in the /drush
  17. * folder in the directory above the Drupal root.
  18. */
  19. /**
  20. * Implements hook_drush_command().
  21. *
  22. * In this hook, you specify which commands your
  23. * drush module makes available, what it does and
  24. * description.
  25. *
  26. * Notice how this structure closely resembles how
  27. * you define menu hooks.
  28. *
  29. * See `drush topic docs-commands` for a list of recognized keys.
  30. */
  31. function sandwich_drush_command() {
  32. $items = array();
  33. // The 'make-me-a-sandwich' command.
  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.). Defaults to ascii.',
  38. ),
  39. 'options' => array(
  40. 'spreads' => array(
  41. 'description' => 'Comma delimited list of spreads.',
  42. 'example-value' => 'mayonnaise,mustard',
  43. ),
  44. ),
  45. 'examples' => array(
  46. 'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
  47. ),
  48. 'aliases' => array('mmas'),
  49. // No bootstrap at all.
  50. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  51. );
  52. // The 'sandwiches-served' command. Informs how many 'mmas' commands
  53. // completed.
  54. $items['sandwiches-served'] = array(
  55. 'description' => "Report how many sandwiches we have made.",
  56. 'examples' => array(
  57. 'drush sandwiches-served' => 'Show how many sandwiches we have served.',
  58. ),
  59. 'aliases' => array('sws'),
  60. // Example output engine data: command returns a single keyed
  61. // data item (e.g. array("served" => 1)) that can either be
  62. // printed with a label (e.g. "served: 1"), or output raw with
  63. // --pipe (e.g. "1").
  64. 'engines' => array(
  65. 'outputformat' => array(
  66. 'default' => 'key-value',
  67. 'pipe-format' => 'string',
  68. 'label' => 'Sandwiches Served',
  69. 'require-engine-capability' => array('format-single'),
  70. ),
  71. ),
  72. // No bootstrap at all.
  73. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  74. );
  75. // The 'spreads-status' command. Prints a table about available spreads.
  76. $items['spreads-status'] = array(
  77. 'description' => "Show a table of information about available spreads.",
  78. 'examples' => array(
  79. 'drush spreads-status' => 'Show a table of spreads.',
  80. ),
  81. 'aliases' => array('sps'),
  82. // Example output engine data: command returns a deep array
  83. // that can either be printed in table format or as a json array.
  84. 'engines' => array(
  85. 'outputformat' => array(
  86. 'default' => 'table',
  87. 'pipe-format' => 'json',
  88. // Commands that return deep arrays will usually use
  89. // machine-ids for the column data. A 'field-labels'
  90. // item maps from the machine-id to a human-readable label.
  91. 'field-labels' => array(
  92. 'name' => 'Name',
  93. 'description' => 'Description',
  94. 'available' => 'Num',
  95. 'taste' => 'Taste',
  96. ),
  97. // In table format, the 'column-widths' item is consulted
  98. // to determine the default weights for any named column.
  99. 'column-widths' => array(
  100. 'name' => 10,
  101. 'available' => 3,
  102. ),
  103. 'require-engine-capability' => array('format-table'),
  104. ),
  105. ),
  106. // No bootstrap at all.
  107. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  108. );
  109. // Commandfiles may also add topics. These will appear in
  110. // the list of topics when `drush topic` is executed.
  111. // To view this topic, run `drush --include=/full/path/to/examples topic`
  112. $items['sandwich-exposition'] = array(
  113. 'description' => 'Ruminations on the true meaning and philosophy of sandwiches.',
  114. 'hidden' => TRUE,
  115. 'topic' => TRUE,
  116. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  117. 'callback' => 'drush_print_file',
  118. 'callback arguments' => array(dirname(__FILE__) . '/sandwich-topic.txt'),
  119. );
  120. return $items;
  121. }
  122. /**
  123. * Implements hook_drush_help().
  124. *
  125. * This function is called whenever a drush user calls
  126. * 'drush help <name-of-your-command>'. This hook is optional. If a command
  127. * does not implement this hook, the command's description is used instead.
  128. *
  129. * This hook is also used to look up help metadata, such as help
  130. * category title and summary. See the comments below for a description.
  131. */
  132. function sandwich_drush_help($section) {
  133. switch ($section) {
  134. case 'drush:make-me-a-sandwich':
  135. return dt("This command will make you a delicious sandwich, just how you like it.");
  136. // The 'title' meta item is used to name a group of
  137. // commands in `drush help`. If a title is not defined,
  138. // the default is "All commands in ___", with the
  139. // specific name of the commandfile (e.g. sandwich).
  140. // Command files with less than four commands will
  141. // be placed in the "Other commands" section, _unless_
  142. // they define a title. It is therefore preferable
  143. // to not define a title unless the file defines a lot
  144. // of commands.
  145. case 'meta:sandwich:title':
  146. return dt("Sandwich commands");
  147. // The 'summary' meta item is displayed in `drush help --filter`,
  148. // and is used to give a general idea what the commands in this
  149. // command file do, and what they have in common.
  150. case 'meta:sandwich:summary':
  151. return dt("Automates your sandwich-making business workflows.");
  152. }
  153. }
  154. /**
  155. * Implements drush_hook_COMMAND_validate().
  156. *
  157. * The validate command should exit with
  158. * `return drush_set_error(...)` to stop execution of
  159. * the command. In practice, calling drush_set_error
  160. * OR returning FALSE is sufficient. See drush.api.php
  161. * for more details.
  162. */
  163. function drush_sandwich_make_me_a_sandwich_validate() {
  164. if (drush_is_windows()) {
  165. // $name = drush_get_username();
  166. // @todo Implement check for elevated process using w32api
  167. // as sudo is not available for Windows
  168. // @see http://php.net/manual/en/book.w32api.php
  169. // @see http://social.msdn.microsoft.com/Forums/en/clr/thread/0957c58c-b30b-4972-a319-015df11b427d
  170. }
  171. else {
  172. $name = posix_getpwuid(posix_geteuid());
  173. if ($name['name'] !== 'root') {
  174. return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
  175. }
  176. }
  177. }
  178. /**
  179. * Implements drush_hook_COMMAND().
  180. *
  181. * The command callback is where the action takes place.
  182. *
  183. * The function name should be same as command name but with dashes turned to
  184. * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
  185. * taken from the file 'commandfile.drush.inc', which in this case is
  186. * 'sandwich'. Note also that a simplification step is also done in instances
  187. * where the commandfile name is the same as the beginning of the command name,
  188. * "drush_example_example_foo" is simplified to just "drush_example_foo".
  189. * To also implement a hook that is called before your command, implement
  190. * "drush_hook_pre_example_foo". For a list of all available hooks for a
  191. * given command, run drush in --debug mode.
  192. *
  193. * If for some reason you do not want your hook function to be named
  194. * after your command, you may define a 'callback' item in your command
  195. * object that specifies the exact name of the function that should be
  196. * called.
  197. *
  198. * In this function, all of Drupal's API is (usually) available, including
  199. * any functions you have added in your own modules/themes.
  200. *
  201. * @see drush_invoke()
  202. * @see drush.api.php
  203. */
  204. function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
  205. $str_spreads = '';
  206. // Read options with drush_get_option. Note that the options _must_
  207. // be documented in the $items structure for this command in the 'command'
  208. // hook. See `drush topic docs-commands` for more information.
  209. if ($spreads = drush_get_option('spreads')) {
  210. $list = implode(' and ', explode(',', $spreads));
  211. $str_spreads = ' with just a dash of ' . $list;
  212. }
  213. $msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.',
  214. array('!filling' => $filling, '!str_spreads' => $str_spreads)
  215. );
  216. drush_print("\n" . $msg . "\n");
  217. if (drush_get_context('DRUSH_NOCOLOR')) {
  218. $filename = dirname(__FILE__) . '/sandwich-nocolor.txt';
  219. }
  220. else {
  221. $filename = dirname(__FILE__) . '/sandwich.txt';
  222. }
  223. drush_print(file_get_contents($filename));
  224. // Find out how many sandwiches have been served, and set
  225. // the cached value to one greater.
  226. $served = drush_sandwich_sandwiches_served();
  227. drush_cache_set(drush_get_cid('sandwiches-served'), $served + 1);
  228. }
  229. /**
  230. * Implements drush_hook_COMMAND().
  231. *
  232. * Demonstrates how to return a simple value that is transformed by
  233. * the selected formatter to display either with a label (using the
  234. * key-value formatter) or as the raw value itself (using the string formatter).
  235. */
  236. function drush_sandwich_sandwiches_served() {
  237. $served = 0;
  238. $served_object = drush_cache_get(drush_get_cid('sandwiches-served'));
  239. if ($served_object) {
  240. $served = $served_object->data;
  241. }
  242. // In the default format, key-value, this return value
  243. // will print " Sandwiches Served : 1". In the default pipe
  244. // format, only the array value ("1") is returned.
  245. return $served;
  246. }
  247. /**
  248. * Implements drush_hook_COMMAND().
  249. *
  250. * This ficticious command shows how a deep array can be constructed
  251. * and used as a command return value that can be output by different
  252. * output formatters.
  253. */
  254. function drush_sandwich_spreads_status() {
  255. return array(
  256. 'ketchup' => array(
  257. 'name' => 'Ketchup',
  258. 'description' => 'Some say its a vegetable, but we know its a sweet spread.',
  259. 'available' => '7',
  260. 'taste' => 'sweet',
  261. ),
  262. 'mayonnaise' => array(
  263. 'name' => 'Mayonnaise',
  264. 'description' => 'A nice dairy-free spead.',
  265. 'available' => '12',
  266. 'taste' => 'creamy',
  267. ),
  268. 'mustard' => array(
  269. 'name' => 'Mustard',
  270. 'description' => 'Pardon me, but could you please pass that plastic yellow bottle?',
  271. 'available' => '8',
  272. 'taste' => 'tangy',
  273. ),
  274. 'pickles' => array(
  275. 'name' => 'Pickles',
  276. 'description' => 'A necessary part of any sandwich that does not taste terrible.',
  277. 'available' => '63',
  278. 'taste' => 'tasty',
  279. ),
  280. );
  281. }
  282. /**
  283. * Command argument complete callback.
  284. *
  285. * Provides argument values for shell completion.
  286. *
  287. * @return array
  288. * Array of popular fillings.
  289. */
  290. function sandwich_make_me_a_sandwich_complete() {
  291. return array('values' => array('turkey', 'cheese', 'jelly', 'butter'));
  292. }