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.

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().
drush_sandwich_sandwiches_served Implementation of hook_drush_command() for sandwiches-served command.
drush_sandwich_spreads_status Implementation of hook_drush_command() for spreads-status command.
sandwich_drush_command Implementation of hook_drush_command().
sandwich_drush_help Implementation of hook_drush_help().
sandwich_make_me_a_sandwich_complete Command argument complete callback. Provides argument values for shell completion.

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