Skip to content

Commit

Permalink
Merge pull request #13 from AbdelkaderBah/main
Browse files Browse the repository at this point in the history
Added confirmation prompt
  • Loading branch information
freekmurze authored Jun 10, 2022
2 parents 162537f + 508d33d commit 283cc20
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ return [
* when executing the `remote` command.
*/
'default_host' => 'default',

/*
* When set to true, A confirmation prompt will be shown before executing the `remote` command.
*/
'needs_confirmation' => env('REMOTE_NEEDS_CONFIRMATION', false),

/*
* Here you can define the hosts where the commands should be executed.
Expand Down
5 changes: 5 additions & 0 deletions config/remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
*/
'default_host' => 'default',

/*
* When set to true, A confirmation prompt will be shown before executing the `remote` command.
*/
'needs_confirmation' => env('REMOTE_NEEDS_CONFIRMATION', false),

/*
* Here you can define the hosts where the commands should be executed.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Commands/RemoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function handle()

$commandsToExecute = $this->getCommandsToExecute($hostConfig);

if ($this->failsConfirmationPrompt($hostConfig)) {
return $this->failedConfirmationPromptOutput();
}

if ($this->option('debug')) {
$this->line($ssh->getExecuteCommand($commandsToExecute));

Expand Down Expand Up @@ -71,4 +75,22 @@ protected function displayOutput($type, $line): void
$this->output->write('<fg=red>' . trim($line) . '</>' . PHP_EOL);
}
}

protected function failsConfirmationPrompt(HostConfig $hostConfig): ?bool
{
if (!config('remote.needs_confirmation')) {
return false;
}

return !$this->confirm(
"Are you sure you want to execute this command on the following remote server {$hostConfig->host}?"
);
}

protected function failedConfirmationPromptOutput(): int
{
$this->error('Remote command aborted');

return 0;
}
}
25 changes: 25 additions & 0 deletions tests/RemoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ public function it_can_execute_a_remote_command()
$this->assertMatchesSnapshot(Artisan::output());
}

/** @test */
public function it_cannot_execute_a_remote_command_without_confirming_when_confirmation_option_is_on()
{
config()->set('remote.needs_confirmation', true);

$host = config('remote.hosts.default.host');

$this->artisan('remote test --debug')
->expectsConfirmation("Are you sure you want to execute this command on the following remote server {$host}?", 'no')
->expectsOutput('Remote command aborted')
->assertExitCode(0);
}

/** @test */
public function it_can_execute_a_remote_command_with_confirming_when_confirmation_option_is_on()
{
config()->set('remote.needs_confirmation', true);

$host = config('remote.hosts.default.host');

$this->artisan('remote test --debug')
->expectsConfirmation("Are you sure you want to execute this command on the following remote server {$host}?", 'yes')
->doesntExpectOutput("Remote command aborted");
}

/** @test */
public function it_can_execute_a_raw_command()
{
Expand Down

0 comments on commit 283cc20

Please sign in to comment.