Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Console/Server/ServerAddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->hr();

$this->displayServerInfo($server);
$this->displayServerDeets($server);

//
// Verify connectivity
Expand Down
56 changes: 10 additions & 46 deletions app/Console/Server/ServerDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Bigpixelrocket\DeployerPHP\Console\Server;

use Bigpixelrocket\DeployerPHP\Contracts\BaseCommand;
use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO;
use Bigpixelrocket\DeployerPHP\Traits\ServerHelpersTrait;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -44,54 +43,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->hr();

//
// Get all servers

$allServers = $this->servers->all();
if (count($allServers) === 0) {
$this->warning('No servers found in inventory');
$this->writeln([
'',
'Use <fg=cyan>server:add</> to add a server',
'',
]);

return Command::SUCCESS;
}

// Extract server names from DTOs for promptSelect
$serverNames = array_map(fn (ServerDTO $server) => $server->name, $allServers);

//
// Select server to delete

$this->h1('Delete Server');

$name = (string) $this->getOptionOrPrompt(
'name',
fn () => $this->promptSelect(
label: 'Select server:',
options: $serverNames,
)
);

//
// Find server and display info

$server = null;
foreach ($allServers as $s) {
if ($s->name === $name) {
$server = $s;
break;
}
}
// Select server

$selection = $this->selectServer();

if ($server === null) {
$this->error("Server '{$name}' not found in inventory");
return Command::FAILURE;
if ($selection['server'] === null) {
return $selection['exit_code'];
}

$this->displayServerInfo($server);
$server = $selection['server'];
$this->displayServerDeets($server);

//
// Confirm deletion
Expand All @@ -115,16 +79,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
//
// Delete server

$this->servers->delete($name);
$this->servers->delete($server->name);

$this->success("Server '{$name}' deleted successfully");
$this->success("Server '{$server->name}' deleted successfully");
$this->writeln('');

//
// Show command hint

$this->showCommandHint('server:delete', [
'name' => $name,
'name' => $server->name,
'yes' => $confirmed,
]);

Expand Down
2 changes: 1 addition & 1 deletion app/Console/Server/ServerListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->h1('All Servers');

foreach ($allServers as $server) {
$this->displayServerInfo($server);
$this->displayServerDeets($server);
}

return Command::SUCCESS;
Expand Down
12 changes: 6 additions & 6 deletions app/SymfonyApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ private function displayBanner(): void
// Simple, compact banner
$banner = [
'',
'<fg=cyan>╭───────</><fg=blue>─────────</><fg=bright-blue>─────────</><fg=magenta>─────────</><fg=gray>────────</>',
' <fg=cyan>┌┬┐┌─┐┌─┐┬ ┌─┐┬ ┬┌─┐┬─┐</>',
' <fg=cyan> ││├┤ ├─┘│ │ │└┬┘├┤ ├┬┘</>',
' <fg=blue>─┴┘└─┘┴ ┴─┘└─┘ ┴ └─┘┴└─PHP</> <fg=bright-blue>'.$version.'</>',
'<fg=cyan;options=bold>╭───────</><fg=blue;options=bold>─────────</><fg=bright-blue;options=bold>─────────</><fg=magenta;options=bold>─────────</><fg=gray;options=bold>─────────</>',
' <fg=cyan;options=bold>┌┬┐┌─┐┌─┐┬ ┌─┐┬ ┬┌─┐┬─┐</>',
' <fg=cyan;options=bold> ││├┤ ├─┘│ │ │└┬┘├┤ ├┬┘</>',
' <fg=blue;options=bold>─┴┘└─┘┴ ┴─┘└─┘ ┴ └─┘┴└─PHP</> <fg=bright-blue;options=bold>'.$version.'</>',
'',
' <fg=gray>The Server & Site Deployment Tool for PHP</>',
'<fg=cyan>╰───────</><fg=blue>─────────</><fg=bright-blue>─────────</><fg=magenta>─────────</><fg=gray>────────</>',
' The Server & Site Deployment Tool for PHP',
'<fg=cyan;options=bold>╰───────</><fg=blue;options=bold>─────────</><fg=bright-blue;options=bold>─────────</><fg=magenta;options=bold>─────────</><fg=gray;options=bold>─────────</>',
''
];

Expand Down
4 changes: 2 additions & 2 deletions app/Traits/ConsoleOutputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function error(string $message): void
protected function h1(string $text): void
{
$this->writeln([
'<fg=bright-blue>▸ </><fg=cyan>'.$text.'</>',
'<fg=bright-blue>▸ </><fg=cyan;options=bold>'.$text.'</>',
'',
]);
}
Expand All @@ -85,7 +85,7 @@ protected function h1(string $text): void
protected function hr(): void
{
$this->writeln([
'<fg=cyan>╭───────</><fg=blue>─────────</><fg=bright-blue>─────────</><fg=magenta>─────────</><fg=gray>────────</>',
'<fg=cyan;options=bold>╭───────</><fg=blue;options=bold>─────────</><fg=bright-blue;options=bold>─────────</><fg=magenta;options=bold>─────────</><fg=gray;options=bold>─────────</>',
'',
]);
}
Expand Down
72 changes: 64 additions & 8 deletions app/Traits/ServerHelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO;
use Bigpixelrocket\DeployerPHP\Repositories\ServerRepository;
use Bigpixelrocket\DeployerPHP\Services\SSHService;
use Symfony\Component\Console\Command\Command;

/**
* Reusable server-related helpers for commands.
Expand All @@ -18,18 +19,73 @@
trait ServerHelpersTrait
{
/**
* Display server information.
* Display server details.
*/
protected function displayServerInfo(ServerDTO $server, string $color = 'black'): void
protected function displayServerDeets(ServerDTO $server): void
{
$this->writeln([
" <fg={$color}>Name: </><fg=gray>{$server->name}</>",
" <fg={$color}>Host: </><fg=gray>{$server->host}</>",
" <fg={$color}>Port: </><fg=gray>{$server->port}</>",
" <fg={$color}>User: </><fg=gray>{$server->username}</>",
" <fg={$color}>Key: </><fg=gray>".($server->privateKeyPath ?? 'default (~/.ssh/id_ed25519 or ~/.ssh/id_rsa)')."</>",
" "
" Name: <fg=gray>{$server->name}</>",
" Host: <fg=gray>{$server->host}</>",
" Port: <fg=gray>{$server->port}</>",
" User: <fg=gray>{$server->username}</>",
' Key: <fg=gray>'.($server->privateKeyPath ?? 'default (~/.ssh/id_ed25519 or ~/.ssh/id_rsa)').'</>',
' '
]);
}

/**
* Select a server from inventory by name option or interactive prompt.
*
* @return array{server: ServerDTO|null, exit_code: int} Server DTO and exit code (SUCCESS if empty inventory, FAILURE if not found)
*/
protected function selectServer(string $optionName = 'name', string $promptLabel = 'Select server:'): array
{
//
// Get all servers

$allServers = $this->servers->all();
if (count($allServers) === 0) {
$this->warning('No servers found in inventory');
$this->writeln([
'',
'Use <fg=cyan>server:add</> to add a server',
'',
]);

return ['server' => null, 'exit_code' => Command::SUCCESS];
}

//
// Extract server names and prompt for selection

$serverNames = array_map(fn (ServerDTO $server) => $server->name, $allServers);

$name = (string) $this->getOptionOrPrompt(
$optionName,
fn () => $this->promptSelect(
label: $promptLabel,
options: $serverNames,
)
);

//
// Find server by name

$server = null;
foreach ($allServers as $s) {
if ($s->name === $name) {
$server = $s;
break;
}
}

if ($server === null) {
$this->error("Server '{$name}' not found in inventory");

return ['server' => null, 'exit_code' => Command::FAILURE];
}

return ['server' => $server, 'exit_code' => Command::SUCCESS];
}

}
2 changes: 1 addition & 1 deletion tests/Fixtures/TestConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'hr' => $this->hr(),
'writeln' => $this->writeln(...$this->testArgs),
'showCommandHint' => $this->showCommandHint(...$this->testArgs),
'displayServerInfo' => $this->displayServerInfo(...$this->testArgs),
'displayServerDeets' => $this->displayServerDeets(...$this->testArgs),
'getOptionOrPrompt' => $this->testGetOptionOrPrompt(),
'getOptionOrPromptEmpty' => $this->testGetOptionOrPromptEmpty(),
'getOptionOrPromptBoolean' => $this->testGetOptionOrPromptBoolean(),
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Traits/ServerHelpersTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
});

//
// displayServerInfo
// displayServerDeets
// -------------------------------------------------------------------------------

it('displays server information with all fields', function () {
// ARRANGE
$this->command->setTestMethod('displayServerInfo', [
$this->command->setTestMethod('displayServerDeets', [
new ServerDTO(
name: 'production-web',
host: '192.168.1.100',
Expand Down Expand Up @@ -51,7 +51,7 @@

it('displays default SSH key message when privateKeyPath is null', function () {
// ARRANGE
$this->command->setTestMethod('displayServerInfo', [
$this->command->setTestMethod('displayServerDeets', [
new ServerDTO(name: 'test-server', host: '127.0.0.1'),
]);

Expand All @@ -68,7 +68,7 @@

it('displays server info with default values', function () {
// ARRANGE
$this->command->setTestMethod('displayServerInfo', [
$this->command->setTestMethod('displayServerDeets', [
new ServerDTO(name: 'minimal', host: 'example.com'),
]);

Expand Down