Skip to content

Commit b26c185

Browse files
lepokleLeo von Klenzeweitzman
authored
Allow stdin to be redirected to ssh command (#6275)
* Allow stdin to be redirected to ssh command As described in #6274, stdin is currently not redirected to ssh command. With this change the command is adapted to be consistent with `sql:cli` and stdin is redirected. * Add usage info / test todo for stdin redirection (#6274) * Update src/Commands/core/SshCommands.php --------- Co-authored-by: Leo von Klenze <[email protected]> Co-authored-by: Moshe Weitzman <[email protected]>
1 parent 570a05d commit b26c185

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Commands/core/SshCommands.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Drush\Commands\core;
66

7+
use Consolidation\AnnotatedCommand\Input\StdinAwareInterface;
8+
use Consolidation\AnnotatedCommand\Input\StdinAwareTrait;
79
use Consolidation\SiteAlias\SiteAliasManagerInterface;
810
use Consolidation\SiteProcess\Util\Shell;
911
use Consolidation\SiteProcess\Util\Tty;
@@ -13,9 +15,10 @@
1315
use Drush\Commands\DrushCommands;
1416

1517
#[CLI\Bootstrap(DrupalBootLevels::NONE)]
16-
final class SshCommands extends DrushCommands
18+
final class SshCommands extends DrushCommands implements StdinAwareInterface
1719
{
1820
use AutowireTrait;
21+
use StdinAwareTrait;
1922

2023
const SSH = 'site:ssh';
2124

@@ -37,6 +40,7 @@ public function __construct(
3740
#[CLI\Usage(name: 'drush @prod ssh "ls /tmp"', description: 'Run <info>ls /tmp</info> on <info>@prod</info> site.')]
3841
#[CLI\Usage(name: 'drush @prod ssh "git pull"', description: 'Run <info>git pull</info> on the Drupal root directory on the <info>@prod</info> site.')]
3942
#[CLI\Usage(name: 'drush ssh "git pull"', description: 'Run <info>git pull</info> on the local Drupal root directory.')]
43+
#[CLI\Usage(name: 'echo \'Deny from all\' | drush @prod ssh "tee > do-not-expose-dir/.htaccess"', description: 'Protect directory <info>do-not-expose-dir</info> from being served by Apache httpd.')]
4044
#[CLI\Topics(topics: [DocsCommands::ALIASES])]
4145
public function ssh(array $code, $options = ['cd' => self::REQ]): void
4246
{
@@ -56,8 +60,10 @@ public function ssh(array $code, $options = ['cd' => self::REQ]): void
5660
}
5761

5862
$process = $this->processManager()->siteProcess($alias, $code);
59-
if (Tty::isTtySupported()) {
60-
$process->setTty($options['tty']);
63+
if (!Tty::isTtySupported()) {
64+
$process->setInput($this->stdin()->getStream());
65+
} else {
66+
$process->setTty($process->setTty($options['tty']));
6167
}
6268
// The transport handles the chdir during processArgs().
6369
$fallback = $alias->hasRoot() ? $alias->root() : null;

tests/functional/SiteSshTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ public function testSshSingleArgs()
7272
$expected = "[notice] Simulating: ssh -o PasswordAuthentication=no user@server 'cd /path/to/drupal && ls /path1 /path2'";
7373
$this->assertStringContainsString($expected, $this->getSimplifiedErrorOutput());
7474
}
75+
76+
/**
77+
* @todo If this becomes an integration test, add test for stdin handling.
78+
*/
7579
}

0 commit comments

Comments
 (0)