Skip to content

Commit d581fdc

Browse files
committed
Refactor SshClient
1 parent 38eb101 commit d581fdc

File tree

9 files changed

+15
-47
lines changed

9 files changed

+15
-47
lines changed

contrib/rsync.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113

114114
namespace Deployer;
115115

116-
use Deployer\Component\Ssh\Client;
117116
use Deployer\Host\Localhost;
118117
use Deployer\Task\Context;
119118

src/Component/ProcessRunner/Printer.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,11 @@ public function printBuffer(string $type, Host $host, string $buffer): void
5959

6060
public function writeln(string $type, Host $host, string $line): void
6161
{
62-
$line = self::filterOutput($line);
63-
6462
// Omit empty lines
6563
if (empty($line)) {
6664
return;
6765
}
6866

6967
$this->output->writeln("[$host] $line");
7068
}
71-
72-
public static function filterOutput(string $output): string
73-
{
74-
return preg_replace('/\[exit_code:(.*?)]/', '', $output);
75-
}
7669
}

src/Deployer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Deployer\Component\Pimple\Container;
2525
use Deployer\Component\ProcessRunner\Printer;
2626
use Deployer\Component\ProcessRunner\ProcessRunner;
27-
use Deployer\Component\Ssh\Client;
27+
use Deployer\Ssh\SshClient;
2828
use Deployer\Configuration\Configuration;
2929
use Deployer\Executor\Master;
3030
use Deployer\Executor\Messenger;
@@ -58,7 +58,7 @@
5858
* @property HostCollection|Host[] $hosts
5959
* @property Configuration $config
6060
* @property Rsync $rsync
61-
* @property Client $sshClient
61+
* @property SshClient $sshClient
6262
* @property ProcessRunner $processRunner
6363
* @property Task\ScriptManager $scriptManager
6464
* @property Selector $selector
@@ -128,7 +128,7 @@ public function __construct(Application $console)
128128
return new Printer($c['output']);
129129
};
130130
$this['sshClient'] = function ($c) {
131-
return new Client($c['output'], $c['pop'], $c['logger']);
131+
return new SshClient($c['output'], $c['pop'], $c['logger']);
132132
};
133133
$this['rsync'] = function ($c) {
134134
return new Rsync($c['pop'], $c['output']);

src/Executor/Master.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010

1111
namespace Deployer\Executor;
1212

13-
use Deployer\Component\Ssh\Client;
14-
use Deployer\Component\Ssh\IOArguments;
1513
use Deployer\Deployer;
16-
use Deployer\Exception\Exception;
1714
use Deployer\Host\Host;
1815
use Deployer\Host\HostCollection;
19-
use Deployer\Host\Localhost;
2016
use Deployer\Selector\Selector;
17+
use Deployer\Ssh\IOArguments;
2118
use Deployer\Task\Context;
2219
use Deployer\Task\Task;
2320
use Symfony\Component\Console\Input\InputInterface;

src/Logger/Logger.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public function printBuffer(Host $host, string $type, string $buffer): void
4747

4848
public function writeln(Host $host, string $type, string $line): void
4949
{
50-
$line = Printer::filterOutput($line);
51-
5250
// Omit empty lines
5351
if (empty($line)) {
5452
return;

src/Component/Ssh/IOArguments.php renamed to src/Ssh/IOArguments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Deployer\Component\Ssh;
11+
namespace Deployer\Ssh;
1212

1313
use Deployer\Exception\Exception;
1414
use Symfony\Component\Console\Input\InputInterface;

src/Component/Ssh/Client.php renamed to src/Ssh/SshClient.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Deployer\Component\Ssh;
11+
namespace Deployer\Ssh;
1212

1313
use Deployer\Component\ProcessRunner\Printer;
14-
use Deployer\Exception\Exception;
1514
use Deployer\Exception\RunException;
1615
use Deployer\Exception\TimeoutException;
1716
use Deployer\Host\Host;
@@ -20,24 +19,11 @@
2019
use Symfony\Component\Process\Exception\ProcessTimedOutException;
2120
use Symfony\Component\Process\Process;
2221

23-
use function Deployer\Support\parse_home_dir;
24-
25-
class Client
22+
class SshClient
2623
{
27-
/**
28-
* @var OutputInterface
29-
*/
30-
private $output;
31-
32-
/**
33-
* @var Printer
34-
*/
35-
private $pop;
36-
37-
/**
38-
* @var Logger
39-
*/
40-
private $logger;
24+
private OutputInterface $output;
25+
private Printer $pop;
26+
private Logger $logger;
4127

4228
public function __construct(OutputInterface $output, Printer $pop, Logger $logger)
4329
{
@@ -46,9 +32,6 @@ public function __construct(OutputInterface $output, Printer $pop, Logger $logge
4632
$this->logger = $logger;
4733
}
4834

49-
/**
50-
* @throws RunException|TimeoutException|Exception
51-
*/
5235
public function run(Host $host, string $command, array $config = []): string
5336
{
5437
$defaults = [
@@ -59,7 +42,7 @@ public function run(Host $host, string $command, array $config = []): string
5942
];
6043
$config = array_merge($defaults, $config);
6144

62-
$shellId = bin2hex(random_bytes(10));
45+
$shellId = 'id$' . bin2hex(random_bytes(10));
6346
$shellCommand = $host->getShell();
6447
if ($host->has('become') && !empty($host->get('become'))) {
6548
$shellCommand = "sudo -H -u {$host->get('become')} " . $shellCommand;
@@ -84,7 +67,7 @@ public function run(Host $host, string $command, array $config = []): string
8467

8568
$process = new Process($ssh);
8669
$process
87-
->setInput("( $command ); printf '[exit_code:%s]' $?;")
70+
->setInput($command)
8871
->setTimeout((null === $config['timeout']) ? null : (float) $config['timeout'])
8972
->setIdleTimeout((null === $config['idle_timeout']) ? null : (float) $config['idle_timeout']);
9073

@@ -106,8 +89,8 @@ public function run(Host $host, string $command, array $config = []): string
10689
);
10790
}
10891

109-
$output = $this->pop->filterOutput($process->getOutput());
110-
$exitCode = $this->parseExitStatus($process);
92+
$output = $process->getOutput();
93+
$exitCode = $process->getExitCode();
11194

11295
if ($exitCode !== 0 && !$config['no_throw']) {
11396
throw new RunException(

src/Utility/Rsync.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
namespace Deployer\Utility;
1212

1313
use Deployer\Component\ProcessRunner\Printer;
14-
use Deployer\Component\Ssh\Client;
1514
use Deployer\Exception\RunException;
1615
use Deployer\Host\Host;
1716
use Symfony\Component\Console\Helper\ProgressBar;
1817
use Symfony\Component\Console\Output\OutputInterface;
1918
use Symfony\Component\Process\Exception\ProcessFailedException;
2019
use Symfony\Component\Process\Process;
21-
2220
use function Deployer\writeln;
2321

2422
class Rsync

tests/src/Component/Ssh/IOArgumentsTest.php renamed to tests/src/Ssh/IOArgumentsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Deployer\Component\Ssh;
3+
namespace Deployer\Ssh;
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Console\Input\ArgvInput;

0 commit comments

Comments
 (0)