diff --git a/README.md b/README.md index 2668ca0..b22fc37 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ return [ // 'port' => '', // 'username' => '', // 'private_key' => '', + // 'private_key_path' => '', // 'passphrase' => '', // 'script_path' => '', // ], diff --git a/config/task-runner.php b/config/task-runner.php index cc4c75c..dce6a31 100644 --- a/config/task-runner.php +++ b/config/task-runner.php @@ -14,6 +14,7 @@ // 'port' => '', // 'username' => '', // 'private_key' => '', + // 'private_key_path' => '', // 'passphrase' => '', // 'script_path' => '', // ], diff --git a/src/Connection.php b/src/Connection.php index 944a9d9..6b3780c 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -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, diff --git a/src/Helper.php b/src/Helper.php index c560cee..82cf424 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -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'; diff --git a/src/MakesTestAssertions.php b/src/MakesTestAssertions.php index f20db06..e27ab16 100644 --- a/src/MakesTestAssertions.php +++ b/src/MakesTestAssertions.php @@ -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)); @@ -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)); @@ -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(); @@ -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; @@ -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; diff --git a/src/PendingTask.php b/src/PendingTask.php index 5e1e9ae..2d81e0d 100644 --- a/src/PendingTask.php +++ b/src/PendingTask.php @@ -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; @@ -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; @@ -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; @@ -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); diff --git a/src/ProcessOutput.php b/src/ProcessOutput.php index c61b7ae..d9db9df 100644 --- a/src/ProcessOutput.php +++ b/src/ProcessOutput.php @@ -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; diff --git a/src/ProcessRunner.php b/src/ProcessRunner.php index c0aea1b..dd1a986 100644 --- a/src/ProcessRunner.php +++ b/src/ProcessRunner.php @@ -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; diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 31b8219..631d1e6 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -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' => '', ]]); @@ -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'); +}); diff --git a/tests/private_key b/tests/private_key new file mode 100644 index 0000000..ee90847 --- /dev/null +++ b/tests/private_key @@ -0,0 +1 @@ +secret2 \ No newline at end of file