Skip to content

Commit

Permalink
Add missing callback param to ssh functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tigitz committed Apr 19, 2024
1 parent 2d5f83d commit 2d67aad
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions examples/ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
);
}
13 changes: 9 additions & 4 deletions src/Runner/SshRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public function execute(
?bool $allowFailure = null,
?bool $notify = null,
?float $timeout = null,
?callable $callback = null,
): Process {
$ssh = $this->buildSsh($host, $user, $sshOptions);

if ($path) {
$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 */
Expand All @@ -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 */
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -83,7 +87,8 @@ private function run(
timeout: $timeout,
quiet: $quiet,
allowFailure: $allowFailure,
notify: $notify
notify: $notify,
callback: $callback
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Generated/ListTest.php.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/Generated/SshRealTimeOutputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Castor\Tests\Generated;

use Castor\Tests\TaskTestCase;
use Symfony\Component\Process\Exception\ProcessFailedException;

class SshRealTimeOutputTest extends TaskTestCase
{
// ssh:real-time-output
public function test(): void
{
$process = $this->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());
}
}
9 changes: 9 additions & 0 deletions tests/Generated/SshRealTimeOutputTest.php.err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
In SshRunner.php line XXXX:

The command "ssh [email protected] 'bash -se' << \EOF-SPATIE-SSH
ls -alh
EOF-SPATIE-SSH" failed.


ssh:real-time-output

2 changes: 2 additions & 0 deletions tests/Generated/SshRealTimeOutputTest.php.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REAL TIME OUTPUT> ssh: Could not resolve hostname server-1.example.com: Name or service not known

0 comments on commit 2d67aad

Please sign in to comment.