Skip to content

Commit

Permalink
Merge pull request #9 from deliciousbrains/get-server-command
Browse files Browse the repository at this point in the history
Get server command
  • Loading branch information
A5hleyRich authored Nov 23, 2021
2 parents dbe07e4 + 92010e6 commit b4e2da8
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 3 deletions.
52 changes: 50 additions & 2 deletions app/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Collection;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\Table;

abstract class BaseCommand extends Command
{
Expand All @@ -18,6 +19,10 @@ abstract class BaseCommand extends Command

protected bool $requiresToken = true;

protected bool $largeOutput = false;

protected array $columnsMaxWidths = [];

public function __construct(Configuration $configuration, SpinupWp $spinupWp)
{
parent::__construct();
Expand Down Expand Up @@ -80,12 +85,17 @@ protected function profile(): string

protected function format($resource): void
{
if (empty($resource) || $resource->isEmpty()) {
if (empty($resource) || ($resource instanceof Collection && $resource->isEmpty())) {
return;
}

$this->setStyles();

if ($this->displayFormat() === 'table' && $this->largeOutput) {
$this->largeOutput($resource);
return;
}

if ($this->displayFormat() === 'table') {
$this->toTable($resource);
return;
Expand Down Expand Up @@ -121,7 +131,10 @@ protected function displayFormat(): string

protected function toJson($resource): void
{
$this->line(json_encode($resource->toArray(), JSON_PRETTY_PRINT));
if (!is_array($resource)) {
$resource = $resource->toArray();
}
$this->line(json_encode($resource, JSON_PRETTY_PRINT));
}

protected function toTable($resource): void
Expand Down Expand Up @@ -164,5 +177,40 @@ protected function toTable($resource): void
);
}

protected function largeOutput(array $resource): void
{
$table = new Table($this->output);
$rows = [];

foreach ($resource as $key => $value) {
$rows[] = ['<info>' . $key . '</info>', $value];
}

$table->setRows($rows)->setStyle('default');

if (!empty($this->columnsMaxWidths)) {
foreach ($this->columnsMaxWidths as $column) {
$table->setColumnMaxWidth($column[0], $column[1]);
}
}

$table->render();
}

protected function formatBytes(int $bytes, int $precision = 1, bool $trueSize = false): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$block = ($trueSize) ? 1024 : 1000;

$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log($block));
$pow = min($pow, count($units) - 1);
$bytes /= pow($block, $pow);

$total = ($trueSize || $precision > 0) ? round($bytes, $precision) : floor($bytes);

return $total . ' ' . $units[$pow];
}

abstract protected function action();
}
51 changes: 51 additions & 0 deletions app/Commands/Servers/GetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Commands\Servers;

use App\Commands\BaseCommand;

class GetCommand extends BaseCommand
{
protected $signature = 'servers:get {server_id} {--format=} {--profile=}';

protected $description = 'Get a server';

protected bool $largeOutput = true;

public function action()
{
$this->columnsMaxWidths[] = [1, 50];

$serverId = $this->argument('server_id');

$server = $this->spinupwp->servers->get($serverId);

if ($this->displayFormat() === 'json') {
return $server;
}

return [
'ID' => $server->id,
'Name' => $server->name,
'Provider Name' => $server->provider_name,
'IP Address' => $server->ip_address,
'SSH Port' => $server->ssh_port,
'Ubuntu' => $server->ubuntu_version,
'Timezone' => $server->timezone,
'Region' => $server->region,
'Size' => $server->size,
'Disk Space' => $this->formatBytes($server->disk_space['used']) . ' of ' . $this->formatBytes($server->disk_space['total'], 0) . ' used',
'Database Server' => $server->database['server'],
'Database Host' => $server->database['host'],
'Database Port' => $server->database['port'],
'SSH Public Key' => $server->ssh_publickey,
'Git Public Key' => $server->git_publickey,
'Connection Status' => ucfirst($server->connection_status),
'Reboot Required' => $server->reboot_required ? 'Yes' : 'No',
'Upgrade Required' => $server->upgrade_required ? 'Yes' : 'No',
'Install Notes' => $server->install_notes ?? '',
'Created At' => $server->created_at,
'Status' => ucfirst($server->status),
];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.4|^8.0",
"deliciousbrains/spinupwp-php-sdk": "^0.3.0"
"deliciousbrains/spinupwp-php-sdk": "^0.4.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.2",
Expand Down
82 changes: 82 additions & 0 deletions tests/Feature/Commands/ServersGetCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

use GuzzleHttp\Psr7\Response;

$response = [

'id' => 1,
'name' => 'hellfish-media',
'provider_name' => 'DigitalOcean',
'ubuntu_version' => '20.04',
'ip_address' => '127.0.0.1',
'disk_space' => [
'total' => 25210576000,
'available' => 17549436000,
'used' => 7661140000,
'updated_at' => '2021-11-03T16:52:48.000000Z',
],
'database' => [
'server' => 'mysql-8.0',
],
'ssh_port' => '22',
'timezone' => 'UTC',
'region' => 'TOR1',
'size' => '1 GB / 1 vCPU',
'database' => [
'server' => 'mysql-8.0',
'host' => 'localhost',
'port' => 3306,
],
'ssh_publickey' => 'ssh-rsa AAAA....',
'git_publickey' => 'ssh-rsa AAAA....',
'connection_status' => 'connected',
'reboot_required' => true,
'upgrade_required' => false,
'install_notes' => null,
'created_at' => '2021-01-01T12:00:00.000000Z',
'status' => 'provisioned',

];
beforeEach(function () use ($response) {
setTestConfigFile();
});

afterEach(function () {
deleteTestConfigFile();
});

test('servers json get command', function () use ($response) {
$this->clientMock->shouldReceive('request')->with('GET', 'servers/1', [])->andReturn(
new Response(200, [], json_encode(['data' => $response]))
);
$this->artisan('servers:get 1')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('servers table get command', function () use ($response) {
$this->clientMock->shouldReceive('request')->with('GET', 'servers/1', [])->andReturn(
new Response(200, [], json_encode(['data' => $response]))
);
$this->artisan('servers:get 1 --format=table')->expectsTable([], [
['ID', '1'],
['Name', 'hellfish-media'],
['Provider Name', 'DigitalOcean'],
['IP Address', '127.0.0.1'],
['SSH Port', '22'],
['Ubuntu', '20.04'],
['Timezone', 'UTC'],
['Region', 'TOR1'],
['Size', '1 GB / 1 vCPU'],
['Disk Space', '7.7 GB of 25 GB used'],
['Database Server', 'mysql-8.0'],
['Database Host', 'localhost'],
['Database Port', '3306'],
['SSH Public Key', 'ssh-rsa AAAA....'],
['Git Public Key', 'ssh-rsa AAAA....'],
['Connection Status', 'Connected'],
['Reboot Required', 'Yes'],
['Upgrade Required', 'No'],
['Install Notes', ''],
['Created At', '2021-01-01T12:00:00.000000Z'],
['Status', 'Provisioned'],
]);
});

0 comments on commit b4e2da8

Please sign in to comment.