From 2d67aad4adab8825c3fcb9352eba9e65e1e139cc Mon Sep 17 00:00:00 2001 From: tigitz Date: Fri, 19 Apr 2024 23:09:58 +0200 Subject: [PATCH] Add missing callback param to ssh functions --- CHANGELOG.md | 1 + examples/ssh.php | 14 ++++++++++++ src/Runner/SshRunner.php | 13 +++++++---- src/functions.php | 3 ++- tests/Generated/ListTest.php.output.txt | 1 + tests/Generated/SshRealTimeOutputTest.php | 22 +++++++++++++++++++ .../SshRealTimeOutputTest.php.err.txt | 9 ++++++++ .../SshRealTimeOutputTest.php.output.txt | 2 ++ 8 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/Generated/SshRealTimeOutputTest.php create mode 100644 tests/Generated/SshRealTimeOutputTest.php.err.txt create mode 100644 tests/Generated/SshRealTimeOutputTest.php.output.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index aac10bbf..8534d3f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Deprecate `AfterApplicationInitializationEvent` event. Use `FunctionsResolvedEvent` instead. * Fix multiple remote imports of the same package with default version +* Add `?callable $callback = null` param to `ssh_*` functions ## 0.15.0 (2024-04-03) diff --git a/examples/ssh.php b/examples/ssh.php index 81302912..9ed8bb94 100644 --- a/examples/ssh.php +++ b/examples/ssh.php @@ -4,6 +4,7 @@ use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\ssh_download; use function Castor\ssh_run; use function Castor\ssh_upload; @@ -35,3 +36,16 @@ function download(): void { ssh_download('/tmp/test.html', '/var/www/index.html', host: 'server-1.example.com', user: 'debian'); } + +#[AsTask(description: 'Output in real-time ssh command output')] +function realTimeOutput(): void +{ + ssh_run( + command: 'ls -alh', + host: 'server-1.example.com', + user: 'debian', + callback: function ($type, $buffer): void { + io()->writeln('REAL TIME OUTPUT> ' . $buffer); + } + ); +} diff --git a/src/Runner/SshRunner.php b/src/Runner/SshRunner.php index 34cb1fd5..0ec19576 100644 --- a/src/Runner/SshRunner.php +++ b/src/Runner/SshRunner.php @@ -24,6 +24,7 @@ public function execute( ?bool $allowFailure = null, ?bool $notify = null, ?float $timeout = null, + ?callable $callback = null, ): Process { $ssh = $this->buildSsh($host, $user, $sshOptions); @@ -31,7 +32,7 @@ public function execute( $command = sprintf('cd %s && %s', $path, $command); } - return $this->run($ssh->getExecuteCommand($command), $quiet, $allowFailure, $notify, $timeout); + return $this->run($ssh->getExecuteCommand($command), $quiet, $allowFailure, $notify, $timeout, $callback); } /** @phpstan-param SshOptions $sshOptions */ @@ -45,10 +46,11 @@ public function upload( ?bool $allowFailure = null, ?bool $notify = null, ?float $timeout = null, + ?callable $callback = null, ): Process { $ssh = $this->buildSsh($host, $user, $sshOptions); - return $this->run($ssh->getUploadCommand($sourcePath, $destinationPath), $quiet, $allowFailure, $notify, $timeout); + return $this->run($ssh->getUploadCommand($sourcePath, $destinationPath), $quiet, $allowFailure, $notify, $timeout, $callback); } /** @phpstan-param SshOptions $sshOptions */ @@ -62,10 +64,11 @@ public function download( ?bool $allowFailure = null, ?bool $notify = null, ?float $timeout = null, + ?callable $callback = null, ): Process { $ssh = $this->buildSsh($host, $user, $sshOptions); - return $this->run($ssh->getDownloadCommand($sourcePath, $destinationPath), $quiet, $allowFailure, $notify, $timeout); + return $this->run($ssh->getDownloadCommand($sourcePath, $destinationPath), $quiet, $allowFailure, $notify, $timeout, $callback); } private function run( @@ -74,6 +77,7 @@ private function run( ?bool $allowFailure = null, ?bool $notify = null, ?float $timeout = null, + ?callable $callback = null, ): Process { return $this->processRunner->run( $command, @@ -83,7 +87,8 @@ private function run( timeout: $timeout, quiet: $quiet, allowFailure: $allowFailure, - notify: $notify + notify: $notify, + callback: $callback ); } diff --git a/src/functions.php b/src/functions.php index bf29a590..7e715cda 100644 --- a/src/functions.php +++ b/src/functions.php @@ -182,8 +182,9 @@ function ssh_run( ?bool $allowFailure = null, ?bool $notify = null, ?float $timeout = null, + ?callable $callback = null, ): Process { - return Container::get()->sshRunner->execute($command, $path, $host, $user, $sshOptions, $quiet, $allowFailure, $notify, $timeout); + return Container::get()->sshRunner->execute($command, $path, $host, $user, $sshOptions, $quiet, $allowFailure, $notify, $timeout, $callback); } /** diff --git a/tests/Generated/ListTest.php.output.txt b/tests/Generated/ListTest.php.output.txt index 0f0822a3..f30a3bfc 100644 --- a/tests/Generated/ListTest.php.output.txt +++ b/tests/Generated/ListTest.php.output.txt @@ -71,6 +71,7 @@ shell:sh Runs a sh signal:sigusr2 Captures SIGUSR2 signal ssh:download Downloads a file from the remote server ssh:ls Lists content of /var/www directory on the remote server +ssh:real-time-output Output in real-time ssh command output ssh:upload Uploads a file to the remote server ssh:whoami Connect to a remote server without specifying a user symfony:greet diff --git a/tests/Generated/SshRealTimeOutputTest.php b/tests/Generated/SshRealTimeOutputTest.php new file mode 100644 index 00000000..939e8bd0 --- /dev/null +++ b/tests/Generated/SshRealTimeOutputTest.php @@ -0,0 +1,22 @@ +runTask(['ssh:real-time-output']); + + if (1 !== $process->getExitCode()) { + throw new ProcessFailedException($process); + } + + $this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); + $this->assertStringEqualsFile(__FILE__ . '.err.txt', $process->getErrorOutput()); + } +} diff --git a/tests/Generated/SshRealTimeOutputTest.php.err.txt b/tests/Generated/SshRealTimeOutputTest.php.err.txt new file mode 100644 index 00000000..39bcfd07 --- /dev/null +++ b/tests/Generated/SshRealTimeOutputTest.php.err.txt @@ -0,0 +1,9 @@ +In SshRunner.php line XXXX: + + The command "ssh debian@server-1.example.com 'bash -se' << \EOF-SPATIE-SSH + ls -alh + EOF-SPATIE-SSH" failed. + + +ssh:real-time-output + diff --git a/tests/Generated/SshRealTimeOutputTest.php.output.txt b/tests/Generated/SshRealTimeOutputTest.php.output.txt new file mode 100644 index 00000000..624cbdcc --- /dev/null +++ b/tests/Generated/SshRealTimeOutputTest.php.output.txt @@ -0,0 +1,2 @@ +REAL TIME OUTPUT> ssh: Could not resolve hostname server-1.example.com: Name or service not known +