Skip to content

Commit

Permalink
Merge pull request #5 from deliciousbrains/list-sites-command
Browse files Browse the repository at this point in the history
List sites command
  • Loading branch information
A5hleyRich authored Nov 16, 2021
2 parents 23be4d8 + 22c920e commit 4b8432f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 19 deletions.
30 changes: 26 additions & 4 deletions app/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Client;
use Illuminate\Support\Collection;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

abstract class BaseCommand extends Command
{
Expand Down Expand Up @@ -77,12 +78,33 @@ protected function profile(): string
return 'default';
}

protected function format($resource)
protected function format($resource): void
{
$this->setStyles();

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

$this->toJson($resource);
}

protected function setStyles(): void
{
if (!$this->output->getFormatter()->hasStyle('enabled')) {
$this->output->getFormatter()->setStyle(
'enabled',
new OutputFormatterStyle('green'),
);
}

if (!$this->output->getFormatter()->hasStyle('disabled')) {
$this->output->getFormatter()->setStyle(
'disabled',
new OutputFormatterStyle('red'),
);
}
return $this->toJson($resource);
}

protected function displayFormat(): string
Expand Down Expand Up @@ -123,7 +145,7 @@ protected function toTable($resource): void
$value = '';
}
if (is_bool($value)) {
$value = $value ? 'yes' : 'no';
$value = $value ? '<enabled>Y</enabled>' : '<disabled>N</disabled>';
}
return $value;
}, array_values($item));
Expand Down
10 changes: 6 additions & 4 deletions app/Commands/Servers/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ protected function action()
return $servers;
}

return $servers->map(fn ($item) => [
'ID' => $item->id,
'Name' => $item->name,
'IP Address' => $item->ip_address,
return $servers->map(fn ($server) => [
'ID' => $server->id,
'Name' => $server->name,
'IP Address' => $server->ip_address,
'Ubuntu' => $server->ubuntu_version,
'Database' => $server->database['server'],
]);
}
}
37 changes: 37 additions & 0 deletions app/Commands/Sites/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Commands\Sites;

use App\Commands\BaseCommand;

class ListCommand extends BaseCommand
{
protected $signature = 'sites:list {server_id? : Only list sites belonging to this server} {--format=} {--profile=}';

protected $description = 'Retrieves a list of sites';

protected function action()
{
$serverId = $this->argument('server_id');

if ($serverId) {
$sites = collect($this->spinupwp->sites->listForServer((int) $serverId));
} else {
$sites = collect($this->spinupwp->sites->list());
}

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

return $sites->map(fn ($site) => [
'ID' => $site->id,
'Server ID' => $site->server_id,
'Domain' => $site->domain,
'Site User' => $site->site_user,
'PHP' => $site->php_version,
'Page Cache' => $site->page_cache['enabled'],
'HTTPS' => $site->https['enabled'],
]);
}
}
34 changes: 23 additions & 11 deletions tests/Feature/Commands/ServersListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,36 @@

$response = [
[
'id' => 1,
'name' => 'hellfish-media',
'ip_address' => '127.0.0.1',
'provider_name' => 'DigitalOcean',
'disk_space' => [
'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',
],
],
[
'id' => 2,
'name' => 'staging.hellfish-media',
'ip_address' => '127.0.0.1',
'provider_name' => 'DigitalOcean',
'disk_space' => [
'id' => 2,
'name' => 'staging.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',
],
],
];
beforeEach(function () use ($response) {
Expand Down Expand Up @@ -54,17 +62,21 @@

test('servers table list command', function () {
$this->artisan('servers:list --format table')->expectsTable(
['ID', 'Name', 'IP Address'],
['ID', 'Name', 'IP Address', 'Ubuntu', 'Database'],
[
[
'1',
'hellfish-media',
'127.0.0.1',
'20.04',
'mysql-8.0',
],
[
'2',
'staging.hellfish-media',
'127.0.0.1',
'20.04',
'mysql-8.0',
],
]
);
Expand Down
80 changes: 80 additions & 0 deletions tests/Feature/Commands/SitesListCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use GuzzleHttp\Psr7\Response;

$response = [
[
'id' => 1,
'server_id' => 1,
'domain' => 'hellfishmedia.com',
'site_user' => 'hellfish',
'php_version' => '8.0',
'page_cache' => [
'enabled' => true,
],
'https' => [
'enabled' => true,
],
],
[
'id' => 2,
'server_id' => 2,
'domain' => 'staging.hellfishmedia.com',
'site_user' => 'staging-hellfish',
'php_version' => '8.0',
'page_cache' => [
'enabled' => false,
],
'https' => [
'enabled' => false,
],
],
];
beforeEach(function () use ($response) {
setTestConfigFile();
$this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn(
new Response(200, [], json_encode([
'data' => $response,
]))
);
});

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

it('list command with no api token configured', function () {
$this->spinupwp->setApiKey('');
$this->artisan('sites:list --profile=johndoe')
->assertExitCode(1);
});

test('sites json list command', function () use ($response) {
$this->artisan('sites:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT));
});

test('sites table list command', function () {
$this->artisan('sites:list --format table')->expectsTable(
['ID', 'Server ID', 'Domain', 'Site User', 'PHP', 'Page Cache', 'HTTPS'],
[
[
1,
1,
'hellfishmedia.com',
'hellfish',
'8.0',
'Y',
'Y',
],
[
2,
2,
'staging.hellfishmedia.com',
'staging-hellfish',
'8.0',
'N',
'N',
],
]
);
});

0 comments on commit 4b8432f

Please sign in to comment.