Skip to content

Commit 1d65402

Browse files
committed
Support for private key path
1 parent 18af8fa commit 1d65402

10 files changed

+31
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ return [
159159
// 'port' => '',
160160
// 'username' => '',
161161
// 'private_key' => '',
162+
// 'private_key_path' => '',
162163
// 'passphrase' => '',
163164
// 'script_path' => '',
164165
// ],

config/task-runner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// 'port' => '',
1515
// 'username' => '',
1616
// 'private_key' => '',
17+
// 'private_key_path' => '',
1718
// 'passphrase' => '',
1819
// 'script_path' => '',
1920
// ],

src/Connection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public static function fromArray(array $config): static
9090
$privateKey = $privateKey();
9191
}
9292

93+
if (! $privateKey && array_key_exists('private_key_path', $config)) {
94+
$privateKey = file_get_contents($config['private_key_path']);
95+
}
96+
9397
return new static(
9498
host: $config['host'] ?: null,
9599
port: $config['port'] ?: null,

src/Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function temporaryDirectoryPath(string $pathOrFilename = ''): stri
4444
/**
4545
* Use the nohup command to run a script in the background.
4646
*/
47-
public static function scriptInBackground(string $scriptPath, string $outputPath = null, int $timeout = null): string
47+
public static function scriptInBackground(string $scriptPath, ?string $outputPath = null, ?int $timeout = null): string
4848
{
4949
$outputPath = $outputPath ?: '/dev/null';
5050

src/MakesTestAssertions.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
trait MakesTestAssertions
99
{
10-
public function assertDispatched(string|callable $taskClass, callable $additionalCallback = null): self
10+
public function assertDispatched(string|callable $taskClass, ?callable $additionalCallback = null): self
1111
{
1212
$faked = $this->faked($this->makeAssertCallback($taskClass, $additionalCallback));
1313

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

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

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

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

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

47-
protected function typeNameOfFirstParameter(callable $callback = null): ?string
47+
protected function typeNameOfFirstParameter(?callable $callback = null): ?string
4848
{
4949
if (! $callback) {
5050
return null;
@@ -61,14 +61,14 @@ protected function typeNameOfFirstParameter(callable $callback = null): ?string
6161
return $parameters[0]?->getType()?->getName() ?: null;
6262
}
6363

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

6868
return ! $typeName || $typeName === PendingTask::class;
6969
}
7070

71-
protected function makeAssertCallback(string|callable $taskClass, callable $additionalCallback = null)
71+
protected function makeAssertCallback(string|callable $taskClass, ?callable $additionalCallback = null)
7272
{
7373
if (! $additionalCallback) {
7474
$additionalCallback = fn () => true;

src/PendingTask.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getId(): ?string
126126
/**
127127
* Sets the 'id' property.
128128
*/
129-
public function id(string $id = null): self
129+
public function id(?string $id = null): self
130130
{
131131
$this->id = $id;
132132

@@ -152,7 +152,7 @@ public function getOutputPath(): ?string
152152
/**
153153
* Sets the 'outputPath' property.
154154
*/
155-
public function writeOutputTo(string $outputPath = null): self
155+
public function writeOutputTo(?string $outputPath = null): self
156156
{
157157
$this->outputPath = $outputPath;
158158

@@ -162,7 +162,7 @@ public function writeOutputTo(string $outputPath = null): self
162162
/**
163163
* Checks if the given connection is the same as the connection of this task.
164164
*/
165-
public function shouldRunOnConnection(bool|string|Connection|callable $connection = null): bool
165+
public function shouldRunOnConnection(bool|string|Connection|callable|null $connection = null): bool
166166
{
167167
if ($connection === null && $this->connection !== null) {
168168
return true;
@@ -207,7 +207,7 @@ public function storeInTemporaryDirectory(): string
207207
/**
208208
* Dispatches the task to the given task runner.
209209
*/
210-
public function dispatch(TaskDispatcher $taskDispatcher = null): ?ProcessOutput
210+
public function dispatch(?TaskDispatcher $taskDispatcher = null): ?ProcessOutput
211211
{
212212
/** @var TaskDispatcher */
213213
$taskDispatcher = $taskDispatcher ?: app(TaskDispatcher::class);

src/ProcessOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function setIlluminateResult(ProcessResult $result): self
108108
/**
109109
* Setter for the exit code.
110110
*/
111-
public function setExitCode(int $exitCode = null): self
111+
public function setExitCode(?int $exitCode = null): self
112112
{
113113
$this->exitCode = $exitCode;
114114

src/ProcessRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ProcessRunner
1010
/**
1111
* Runs the given process and waits for it to finish.
1212
*/
13-
public function run(PendingProcess $process, callable $onOutput = null): ProcessOutput
13+
public function run(PendingProcess $process, ?callable $onOutput = null): ProcessOutput
1414
{
1515
$output = new ProcessOutput;
1616

tests/ConnectionTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
use ProtoneMedia\LaravelTaskRunner\Connection;
44

5-
function addConnectionToConfig()
5+
function addConnectionToConfig($callable = true, $path = false)
66
{
77
config(['task-runner.connections.production' => [
88
'host' => '1.1.1.1',
99
'port' => '21',
1010
'username' => 'root',
11-
'private_key' => fn () => 'secret',
11+
'private_key' => $callable ? fn () => 'secret' : null,
12+
'private_key_path' => $path ? __DIR__.'/private_key' : null,
1213
'passphrase' => 'password',
1314
'script_path' => '',
1415
]]);
@@ -21,3 +22,11 @@ function addConnectionToConfig()
2122

2223
expect($connection->privateKey)->toBe('secret');
2324
});
25+
26+
it('can resolve a private key from a path', function () {
27+
addConnectionToConfig(callable: false, path: true);
28+
29+
$connection = Connection::fromConfig('production');
30+
31+
expect($connection->privateKey)->toBe('secret2');
32+
});

tests/private_key

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secret2

0 commit comments

Comments
 (0)