forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update /examples for Drush 9 (drush-ops#2629)
- Loading branch information
Showing
17 changed files
with
329 additions
and
765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace Drush\Commands; | ||
|
||
use Consolidation\AnnotatedCommand\CommandData; | ||
use Drush\Commands\DrushCommands; | ||
|
||
/** | ||
* Load this commandfile using the --include option - e.g. `drush --include=/path/to/drush/examples` | ||
*/ | ||
|
||
class PolicyCommands extends DrushCommands { | ||
|
||
/** | ||
* Prevent catastrophic braino. Note that this file has to be local to the | ||
* machine that intitiates the sql-sync command. | ||
* | ||
* hook validate sql-sync | ||
*/ | ||
public function sqlSyncValidate(CommandData $commandData) { | ||
if ($commandData->input()->getArgument('destination') == '@prod') { | ||
throw new \Exception(dt('Per !file, you may never overwrite the production database.', ['!file' => __FILE__])); | ||
} | ||
} | ||
|
||
/** | ||
* Limit rsync operations to production site. | ||
* | ||
* hook validate core-rsync | ||
*/ | ||
public function rsyncValidate(CommandData $commandData) { | ||
if (preg_match("/^@prod/", $commandData->input()->getArgument('destination'))) { | ||
throw new \Exception(dt('Per !file, you may never rsync to the production site.', ['!file' => __FILE__])); | ||
} | ||
} | ||
|
||
/** | ||
* Unauthorized may not execute updates. | ||
* | ||
* @hook validate updatedb | ||
*/ | ||
public function validateUpdateDb(CommandData $commandData) { | ||
if (!$commandData->input()->getOption('secret') == 'mysecret') { | ||
throw new \Exception(dt('UpoateDb command requires a secret token per site policy.')); | ||
} | ||
} | ||
|
||
/** | ||
* @hook option updatedb | ||
* @option secret A required token else user may not run updatedb command. | ||
*/ | ||
public function optionsetUpdateDb() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
namespace Drush\Commands; | ||
|
||
use Consolidation\AnnotatedCommand\CommandData; | ||
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; | ||
|
||
/** | ||
* Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples mmas` | ||
*/ | ||
|
||
class SandwichCommands extends DrushCommands { | ||
|
||
/** | ||
* Makes a delicious sandwich. | ||
* | ||
* @command make-me-a-sandwich | ||
* @param $filling The type of the sandwich (turkey, cheese, etc.). Defaults to ascii. | ||
* @option spreads A comma delimited list of spreads. | ||
* @usage drush mmas turkey --spreads=ketchup,mustard | ||
* Make a terrible-tasting sandwich that is lacking in pickles. | ||
* @aliases mmas | ||
* @bootstrap DRUSH_BOOTSTRAP_NONE | ||
* @complete \SandwichCommands::complete | ||
*/ | ||
public function makeSandwich($filling, $options = ['spreads' => NULL]) { | ||
if ($spreads = _convert_csv_to_array('spreads')) { | ||
$list = implode(' and ', $spreads); | ||
$str_spreads = ' with just a dash of ' . $list; | ||
} | ||
$msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.', | ||
array('!filling' => $filling, '!str_spreads' => $str_spreads) | ||
); | ||
drush_print("\n" . $msg . "\n"); | ||
$this->printFile(__DIR__ . '/sandwich-nocolor.txt'); | ||
} | ||
|
||
/** | ||
* Show a table of information about available spreads. | ||
* | ||
* @command xkcd-spreads | ||
* @aliases xspreads | ||
* @bootstrap DRUSH_BOOTSTRAP_NONE | ||
* @field-labels | ||
* name: Name | ||
* description: Description | ||
* available: Num | ||
* taste: Taste | ||
* | ||
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields | ||
*/ | ||
public function spread($options = ['format' => 'table']) { | ||
$data = array( | ||
'ketchup' => array( | ||
'name' => 'Ketchup', | ||
'description' => 'Some say its a vegetable, but we know its a sweet spread.', | ||
'available' => '7', | ||
'taste' => 'sweet', | ||
), | ||
'mayonnaise' => array( | ||
'name' => 'Mayonnaise', | ||
'description' => 'A nice dairy-free spead.', | ||
'available' => '12', | ||
'taste' => 'creamy', | ||
), | ||
'mustard' => array( | ||
'name' => 'Mustard', | ||
'description' => 'Pardon me, but could you please pass that plastic yellow bottle?', | ||
'available' => '8', | ||
'taste' => 'tangy', | ||
), | ||
'pickles' => array( | ||
'name' => 'Pickles', | ||
'description' => 'A necessary part of any sandwich that does not taste terrible.', | ||
'available' => '63', | ||
'taste' => 'tasty', | ||
), | ||
); | ||
return new RowsOfFields($data); | ||
} | ||
|
||
/** | ||
* Commandfiles may also add topics. These will appear in | ||
* the list of topics when `drush topic` is executed. | ||
* To view the topic below, run `drush --include=/full/path/to/examples topic` | ||
*/ | ||
|
||
/** | ||
* Ruminations on the true meaning and philosophy of sandwiches. | ||
* | ||
* @command sandwich-exposition | ||
* @hidden | ||
* @topic | ||
* @bootstrap DRUSH_BOOTSTRAP_NONE | ||
*/ | ||
public function ruminate() { | ||
self::printFile(__DIR__ . '/sandwich-topic.md'); | ||
} | ||
|
||
/** | ||
* @hook validate make-me-a-sandwich | ||
*/ | ||
public function sandwichValidate(CommandData $commandData) { | ||
$name = posix_getpwuid(posix_geteuid()); | ||
if ($name['name'] !== 'root') { | ||
throw new \Exception(dt('What? Make your own sandwich.')); | ||
} | ||
} | ||
|
||
/** | ||
* Command argument complete callback. | ||
* | ||
* Provides argument values for shell completion. | ||
* | ||
* @return array | ||
* Array of popular fillings. | ||
*/ | ||
function complete() { | ||
return array('values' => array('turkey', 'cheese', 'jelly', 'butter')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
namespace Drush\Commands; | ||
|
||
|
||
use Consolidation\AnnotatedCommand\CommandData; | ||
use Drush\Commands\DrushCommands; | ||
|
||
/** | ||
* Load this commandfile using the --include option - e.g. `drush --include=/path/to/drush/examples` | ||
*/ | ||
|
||
class SyncViaHttpCommands extends DrushCommands { | ||
|
||
/** | ||
* When a hook extends a command with additional options, it must | ||
* implement declare those option(s) in a @hook option like this one. Doing so will add | ||
* the option to the help text for the modified command, and will also | ||
* allow the new option to be specified on the command line. Without | ||
* this, Drush will fail with an error when a user attempts to use | ||
* an unknown option. | ||
* | ||
* @hook option sql-sync | ||
* @option http-sync Copy the database via http instead of rsync. Value is the url that the existing database dump can be found at. | ||
* @option http-sync-user Username for the protected directory containing the sql dump. | ||
* @option http-sync-password Password for the same directory. | ||
*/ | ||
public function optionsetSqlSync() {} | ||
|
||
/** | ||
* During the pre hook, determine if the http-sync option has been | ||
* specified. If it has been, then disable the normal ssh + rsync | ||
* dump-and-transfer that sql-sync usually does, and transfer the | ||
* database dump via an http download. | ||
* | ||
* @hook pre-command sql-sync | ||
*/ | ||
public function preSqlSync(CommandData $commandData) { | ||
$sql_dump_download_url = $commandData->input()->getOption('http-sync'); | ||
if (!empty($sql_dump_download_url)) { | ||
$user = $commandData->input()->getOption('http-sync-user'); | ||
$password = $commandData->input()->getOption('http-sync-password'); | ||
$source_dump_file = $this->downloadFile($sql_dump_download_url, $user, $password); | ||
$commandData->input()->setOption('target-dump', $source_dump_file); | ||
$commandData->input()->setOption('no-dump', TRUE); | ||
$commandData->input()->setOption('no-sync', TRUE); | ||
} | ||
} | ||
|
||
/** | ||
* Downloads a file. | ||
* | ||
* Optionally uses user authentication, using either wget or curl, as available. | ||
*/ | ||
protected function downloadFile($url, $user = FALSE, $password = FALSE, $destination = FALSE, $overwrite = TRUE) { | ||
static $use_wget; | ||
if ($use_wget === NULL) { | ||
$use_wget = drush_shell_exec('which wget'); | ||
} | ||
|
||
$destination_tmp = drush_tempnam('download_file'); | ||
if ($use_wget) { | ||
if ($user && $password) { | ||
drush_shell_exec("wget -q --timeout=30 --user=%s --password=%s -O %s %s", $user, $password, $destination_tmp, $url); | ||
} | ||
else { | ||
drush_shell_exec("wget -q --timeout=30 -O %s %s", $destination_tmp, $url); | ||
} | ||
} | ||
else { | ||
if ($user && $password) { | ||
drush_shell_exec("curl -s -L --connect-timeout 30 --user %s:%s -o %s %s", $user, $password, $destination_tmp, $url); | ||
} | ||
else { | ||
drush_shell_exec("curl -s -L --connect-timeout 30 -o %s %s", $destination_tmp, $url); | ||
} | ||
} | ||
if (!drush_get_context('DRUSH_SIMULATE')) { | ||
if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($url)) { | ||
@file_put_contents($destination_tmp, $file); | ||
} | ||
if (!drush_file_not_empty($destination_tmp)) { | ||
// Download failed. | ||
throw new \Exception(dt("The URL !url could not be downloaded.", array('!url' => $url))); | ||
} | ||
} | ||
if ($destination) { | ||
drush_move_dir($destination_tmp, $destination, $overwrite); | ||
return $destination; | ||
} | ||
return $destination_tmp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace Drush\Commands; | ||
|
||
/** | ||
* Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples xkcd` | ||
*/ | ||
|
||
class XkcdCommands extends DrushCommands { | ||
|
||
/** | ||
* Retrieve and display xkcd cartoons. | ||
* | ||
* @command xkcd-fetch | ||
* @param $search Optional argument to retrieve the cartoons matching an index number, keyword search or "random". If omitted the latest cartoon will be retrieved. | ||
* @option image-viewer Command to use to view images (e.g. xv, firefox). Defaults to "display" (from ImageMagick). | ||
* @option google-custom-search-api-key Google Custom Search API Key, available from https://code.google.com/apis/console/. Default key limited to 100 queries/day globally. | ||
* @usage drush xkcd | ||
* Retrieve and display the latest cartoon. | ||
* @usage drush xkcd sandwich | ||
* Retrieve and display cartoons about sandwiches. | ||
* @usage drush xkcd 123 --image-viewer=eog | ||
* Retrieve and display cartoon #123 in eog. | ||
* @usage drush xkcd random --image-viewer=firefox | ||
* Retrieve and display a random cartoon in Firefox. | ||
* @aliases @xkcd | ||
*/ | ||
public function fetch($search = NULL, $options = ['image-viewer' => 'open', 'google-custom-search-api-key' => NULL]) { | ||
if (empty($search)) { | ||
drush_start_browser('http://xkcd.com'); | ||
} | ||
elseif (is_numeric($search)) { | ||
drush_start_browser('http://xkcd.com/' . $search); | ||
} | ||
elseif ($search == 'random') { | ||
$xkcd_response = @json_decode(file_get_contents('http://xkcd.com/info.0.json')); | ||
if (!empty($xkcd_response->num)) { | ||
drush_start_browser('http://xkcd.com/' . rand(1, $xkcd_response->num)); | ||
} | ||
} | ||
else { | ||
// This uses an API key with a limited number of searches per. | ||
$search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . drush_get_option('google-custom-search-api-key', 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek') . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search)); | ||
if (!empty($search_response->items)) { | ||
foreach ($search_response->items as $item) { | ||
drush_start_browser($item->link); | ||
} | ||
} | ||
else { | ||
throw new \Exception(dt('The search failed or produced no results.')); | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.