forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiteAliasAlterCommands.php
52 lines (46 loc) · 1.89 KB
/
SiteAliasAlterCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Drush\Commands;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Symfony\Component\Console\Input\InputInterface;
/**
* Load this example by using the --include option - e.g. `drush --include=/path/to/drush/examples`
*/
class SiteAliasAlterCommands extends DrushCommands implements SiteAliasManagerAwareInterface
{
use SiteAliasManagerAwareTrait;
/**
* A few example alterations to site aliases.
*
* @hook pre-init *
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Consolidation\AnnotatedCommand\AnnotationData $annotationData
*/
public function alter(InputInterface $input, AnnotationData $annotationData)
{
$self = $this->siteAliasManager()->getSelf();
if ($self->isRemote()) {
// Always pass along ssh keys.
if (!$self->has('ssh.options')) {
// Don't edit the alias - edit the general config service instead.
$this->getConfig()->set('ssh.options', '-o ForwardAgent=yes');
}
// Change the SSH user.
$input->setOption('remote-user', 'mw2');
// Test to see if specific environment really exists in wildcard
// aliases, but only if the target is a specific host.
$host = $self->get('host');
if (preg_match('#\.myserver.com$#', $host)) {
$ip = gethostbyname($host);
// If the return value of gethostbyname equals its input parameter,
// that indicates failure.
if ($host == $ip) {
$aliasName = $self->name();
throw new \Exception("The alias $aliasName refers to an environment that does not exist.");
}
}
}
}
}