Skip to content

Commit

Permalink
Use Site Alias Manager in ssh command.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Aug 25, 2017
1 parent 86fa841 commit 0515d65
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Commands/core/SshCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

use Drush\Commands\DrushCommands;
use Drush\Log\LogLevel;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\SiteAlias\SiteAliasManagerAwareTrait;
use Drush\SiteAlias\SiteAliasName;

class SshCommands extends DrushCommands
class SshCommands extends DrushCommands implements SiteAliasManagerAwareInterface
{
use SiteAliasManagerAwareTrait;

/**
* Connect to a Drupal site's server via SSH.
Expand Down Expand Up @@ -35,6 +39,41 @@ public function ssh(array $args, $options = ['cd' => true])
}
$command = implode(' ', $args);

// TODO: Remove when no longer needed.
if (!$this->hasSiteAliasManager()) {
return $this->legacySsh($command, $options);
}

$alias = $this->siteAliasManager()->getSelf();
if ($alias->isNone()) {
throw new \Exception('A site alias is required. The way you call ssh command has changed to `drush @alias ssh`.');
}

// Local sites run their bash without SSH.
if (!$alias->isRemote()) {
$return = drush_invoke_process('@self', 'core-execute', array($command), array('escape' => false));
return $return['object'];
}

// We have a remote site - build ssh command and run.
$interactive = false;
$cd = $options['cd'];
if (empty($command)) {
$command = 'bash -l';
$interactive = true;
}
$config = $alias->exportConfig();
$site = $config->get('options', []);

$cmd = drush_shell_proc_build($site, $command, $cd, $interactive);
$status = drush_shell_proc_open($cmd);
if ($status != 0) {
throw new \Exception(dt('An error @code occurred while running the command `@command`', array('@command' => $cmd, '@code' => $status)));
}
}

protected function legacySsh($command, $options = ['cd' => true])
{
if (!$alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS')) {
throw new \Exception('A site alias is required. The way you call ssh command has changed to `drush @alias ssh`.');
}
Expand Down
5 changes: 5 additions & 0 deletions src/SiteAlias/AliasRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public function isRemote()
return $this->has('host');
}

public function isNone()
{
return empty($this->root());
}

/**
* Return the 'root' element of this alias if this alias record
* is local.
Expand Down

0 comments on commit 0515d65

Please sign in to comment.