diff --git a/README.md b/README.md index 0328af0..38365f2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/config/remote.php b/config/remote.php index 811dde6..9b71ca6 100644 --- a/config/remote.php +++ b/config/remote.php @@ -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. */ diff --git a/src/Commands/RemoteCommand.php b/src/Commands/RemoteCommand.php index 9284d30..506439a 100644 --- a/src/Commands/RemoteCommand.php +++ b/src/Commands/RemoteCommand.php @@ -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)); @@ -71,4 +75,22 @@ protected function displayOutput($type, $line): void $this->output->write('' . 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; + } } diff --git a/tests/RemoteTest.php b/tests/RemoteTest.php index b88a4a8..003084d 100644 --- a/tests/RemoteTest.php +++ b/tests/RemoteTest.php @@ -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() {