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