Skip to content

Commit

Permalink
Support for private key path
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Dec 7, 2023
1 parent 18af8fa commit 1d65402
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ return [
// 'port' => '',
// 'username' => '',
// 'private_key' => '',
// 'private_key_path' => '',
// 'passphrase' => '',
// 'script_path' => '',
// ],
Expand Down
1 change: 1 addition & 0 deletions config/task-runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// 'port' => '',
// 'username' => '',
// 'private_key' => '',
// 'private_key_path' => '',
// 'passphrase' => '',
// 'script_path' => '',
// ],
Expand Down
4 changes: 4 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public static function fromArray(array $config): static
$privateKey = $privateKey();
}

if (! $privateKey && array_key_exists('private_key_path', $config)) {
$privateKey = file_get_contents($config['private_key_path']);
}

return new static(
host: $config['host'] ?: null,
port: $config['port'] ?: null,
Expand Down
2 changes: 1 addition & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function temporaryDirectoryPath(string $pathOrFilename = ''): stri
/**
* Use the nohup command to run a script in the background.
*/
public static function scriptInBackground(string $scriptPath, string $outputPath = null, int $timeout = null): string
public static function scriptInBackground(string $scriptPath, ?string $outputPath = null, ?int $timeout = null): string
{
$outputPath = $outputPath ?: '/dev/null';

Expand Down
12 changes: 6 additions & 6 deletions src/MakesTestAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait MakesTestAssertions
{
public function assertDispatched(string|callable $taskClass, callable $additionalCallback = null): self
public function assertDispatched(string|callable $taskClass, ?callable $additionalCallback = null): self
{
$faked = $this->faked($this->makeAssertCallback($taskClass, $additionalCallback));

Expand All @@ -19,7 +19,7 @@ public function assertDispatched(string|callable $taskClass, callable $additiona
return $this;
}

public function assertNotDispatched(string|callable $taskClass, callable $additionalCallback = null): self
public function assertNotDispatched(string|callable $taskClass, ?callable $additionalCallback = null): self
{
$faked = $this->faked($this->makeAssertCallback($taskClass, $additionalCallback));

Expand All @@ -31,7 +31,7 @@ public function assertNotDispatched(string|callable $taskClass, callable $additi
return $this;
}

public function assertDispatchedTimes(string|callable $taskClass, int $times = 1, callable $additionalCallback = null): self
public function assertDispatchedTimes(string|callable $taskClass, int $times = 1, ?callable $additionalCallback = null): self
{
$count = $this->faked($this->makeAssertCallback($taskClass, $additionalCallback))->count();

Expand All @@ -44,7 +44,7 @@ public function assertDispatchedTimes(string|callable $taskClass, int $times = 1
return $this;
}

protected function typeNameOfFirstParameter(callable $callback = null): ?string
protected function typeNameOfFirstParameter(?callable $callback = null): ?string
{
if (! $callback) {
return null;
Expand All @@ -61,14 +61,14 @@ protected function typeNameOfFirstParameter(callable $callback = null): ?string
return $parameters[0]?->getType()?->getName() ?: null;
}

protected function callbackExpectsPendingTask(callable $callback = null): bool
protected function callbackExpectsPendingTask(?callable $callback = null): bool
{
$typeName = $this->typeNameOfFirstParameter($callback);

return ! $typeName || $typeName === PendingTask::class;
}

protected function makeAssertCallback(string|callable $taskClass, callable $additionalCallback = null)
protected function makeAssertCallback(string|callable $taskClass, ?callable $additionalCallback = null)
{
if (! $additionalCallback) {
$additionalCallback = fn () => true;
Expand Down
8 changes: 4 additions & 4 deletions src/PendingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getId(): ?string
/**
* Sets the 'id' property.
*/
public function id(string $id = null): self
public function id(?string $id = null): self
{
$this->id = $id;

Expand All @@ -152,7 +152,7 @@ public function getOutputPath(): ?string
/**
* Sets the 'outputPath' property.
*/
public function writeOutputTo(string $outputPath = null): self
public function writeOutputTo(?string $outputPath = null): self
{
$this->outputPath = $outputPath;

Expand All @@ -162,7 +162,7 @@ public function writeOutputTo(string $outputPath = null): self
/**
* Checks if the given connection is the same as the connection of this task.
*/
public function shouldRunOnConnection(bool|string|Connection|callable $connection = null): bool
public function shouldRunOnConnection(bool|string|Connection|callable|null $connection = null): bool
{
if ($connection === null && $this->connection !== null) {
return true;
Expand Down Expand Up @@ -207,7 +207,7 @@ public function storeInTemporaryDirectory(): string
/**
* Dispatches the task to the given task runner.
*/
public function dispatch(TaskDispatcher $taskDispatcher = null): ?ProcessOutput
public function dispatch(?TaskDispatcher $taskDispatcher = null): ?ProcessOutput
{
/** @var TaskDispatcher */
$taskDispatcher = $taskDispatcher ?: app(TaskDispatcher::class);
Expand Down
2 changes: 1 addition & 1 deletion src/ProcessOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function setIlluminateResult(ProcessResult $result): self
/**
* Setter for the exit code.
*/
public function setExitCode(int $exitCode = null): self
public function setExitCode(?int $exitCode = null): self
{
$this->exitCode = $exitCode;

Expand Down
2 changes: 1 addition & 1 deletion src/ProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ProcessRunner
/**
* Runs the given process and waits for it to finish.
*/
public function run(PendingProcess $process, callable $onOutput = null): ProcessOutput
public function run(PendingProcess $process, ?callable $onOutput = null): ProcessOutput
{
$output = new ProcessOutput;

Expand Down
13 changes: 11 additions & 2 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

use ProtoneMedia\LaravelTaskRunner\Connection;

function addConnectionToConfig()
function addConnectionToConfig($callable = true, $path = false)
{
config(['task-runner.connections.production' => [
'host' => '1.1.1.1',
'port' => '21',
'username' => 'root',
'private_key' => fn () => 'secret',
'private_key' => $callable ? fn () => 'secret' : null,
'private_key_path' => $path ? __DIR__.'/private_key' : null,
'passphrase' => 'password',
'script_path' => '',
]]);
Expand All @@ -21,3 +22,11 @@ function addConnectionToConfig()

expect($connection->privateKey)->toBe('secret');
});

it('can resolve a private key from a path', function () {
addConnectionToConfig(callable: false, path: true);

$connection = Connection::fromConfig('production');

expect($connection->privateKey)->toBe('secret2');
});
1 change: 1 addition & 0 deletions tests/private_key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secret2

0 comments on commit 1d65402

Please sign in to comment.