From 24905f133dbf9eb21ef6c9284854bf26c6225c94 Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Tue, 16 Nov 2021 10:45:45 +0000 Subject: [PATCH 1/6] Add sites:list command --- app/Commands/BaseCommand.php | 2 +- app/Commands/Servers/ListCommand.php | 8 +++---- app/Commands/Sites/ListCommand.php | 31 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 app/Commands/Sites/ListCommand.php diff --git a/app/Commands/BaseCommand.php b/app/Commands/BaseCommand.php index 57a5279..b96cb6c 100644 --- a/app/Commands/BaseCommand.php +++ b/app/Commands/BaseCommand.php @@ -123,7 +123,7 @@ protected function toTable($resource): void $value = ''; } if (is_bool($value)) { - $value = $value ? 'yes' : 'no'; + $value = $value ? 'Enabled' : 'Disabled'; } return $value; }, array_values($item)); diff --git a/app/Commands/Servers/ListCommand.php b/app/Commands/Servers/ListCommand.php index 5abccaf..b15cd4e 100644 --- a/app/Commands/Servers/ListCommand.php +++ b/app/Commands/Servers/ListCommand.php @@ -18,10 +18,10 @@ 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, ]); } } diff --git a/app/Commands/Sites/ListCommand.php b/app/Commands/Sites/ListCommand.php new file mode 100644 index 0000000..d212020 --- /dev/null +++ b/app/Commands/Sites/ListCommand.php @@ -0,0 +1,31 @@ +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'], + ]); + } +} From c6fad37638e89b6f68f6e2372c1a82fde10c60e4 Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Tue, 16 Nov 2021 11:11:05 +0000 Subject: [PATCH 2/6] List sites belonging to server --- app/Commands/Sites/ListCommand.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/Commands/Sites/ListCommand.php b/app/Commands/Sites/ListCommand.php index d212020..36e83d0 100644 --- a/app/Commands/Sites/ListCommand.php +++ b/app/Commands/Sites/ListCommand.php @@ -6,13 +6,19 @@ class ListCommand extends BaseCommand { - protected $signature = 'sites:list {--format=} {--profile=}'; + 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() { - $sites = collect($this->spinupwp->sites->list()); + $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; From 55dd72006f62b382d1e6038a4960151c21967d9a Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Tue, 16 Nov 2021 11:44:07 +0000 Subject: [PATCH 3/6] Color enabled/disabled statuses --- app/Commands/BaseCommand.php | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/app/Commands/BaseCommand.php b/app/Commands/BaseCommand.php index b96cb6c..1109363 100644 --- a/app/Commands/BaseCommand.php +++ b/app/Commands/BaseCommand.php @@ -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 { @@ -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 @@ -123,7 +145,7 @@ protected function toTable($resource): void $value = ''; } if (is_bool($value)) { - $value = $value ? 'Enabled' : 'Disabled'; + $value = $value ? 'Y' : 'N'; } return $value; }, array_values($item)); From 9b6e7097b91aeb91325c286a42dc8f6c2ee1c357 Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Tue, 16 Nov 2021 11:56:35 +0000 Subject: [PATCH 4/6] Add ubuntu and database to server list command --- app/Commands/Servers/ListCommand.php | 2 ++ .../Commands/ServersListCommandTest.php | 34 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/Commands/Servers/ListCommand.php b/app/Commands/Servers/ListCommand.php index b15cd4e..349d7dd 100644 --- a/app/Commands/Servers/ListCommand.php +++ b/app/Commands/Servers/ListCommand.php @@ -22,6 +22,8 @@ protected function action() 'ID' => $server->id, 'Name' => $server->name, 'IP Address' => $server->ip_address, + 'Ubuntu' => $server->ubuntu_version, + 'Database' => $server->database['server'], ]); } } diff --git a/tests/Feature/Commands/ServersListCommandTest.php b/tests/Feature/Commands/ServersListCommandTest.php index 2a38dbb..692d149 100644 --- a/tests/Feature/Commands/ServersListCommandTest.php +++ b/tests/Feature/Commands/ServersListCommandTest.php @@ -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) { @@ -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' ], ] ); From b53c946938f636ea41f0abe32eee43dd25363b42 Mon Sep 17 00:00:00 2001 From: Ashley Rich Date: Tue, 16 Nov 2021 12:13:48 +0000 Subject: [PATCH 5/6] Add tests for sites:list command --- .../Feature/Commands/SitesListCommandTest.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/Feature/Commands/SitesListCommandTest.php diff --git a/tests/Feature/Commands/SitesListCommandTest.php b/tests/Feature/Commands/SitesListCommandTest.php new file mode 100644 index 0000000..2961705 --- /dev/null +++ b/tests/Feature/Commands/SitesListCommandTest.php @@ -0,0 +1,80 @@ + 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', + ], + ] + ); +}); From 7cb8bee2e4100946410cce16f1bdd9bfed0f0461 Mon Sep 17 00:00:00 2001 From: A5hleyRich Date: Tue, 16 Nov 2021 12:14:20 +0000 Subject: [PATCH 6/6] Apply php-cs-fixer changes --- app/Commands/Sites/ListCommand.php | 4 ++-- tests/Feature/Commands/ServersListCommandTest.php | 12 ++++++------ tests/Feature/Commands/SitesListCommandTest.php | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/Commands/Sites/ListCommand.php b/app/Commands/Sites/ListCommand.php index 36e83d0..99552a9 100644 --- a/app/Commands/Sites/ListCommand.php +++ b/app/Commands/Sites/ListCommand.php @@ -15,7 +15,7 @@ protected function action() $serverId = $this->argument('server_id'); if ($serverId) { - $sites = collect($this->spinupwp->sites->listForServer((int)$serverId)); + $sites = collect($this->spinupwp->sites->listForServer((int) $serverId)); } else { $sites = collect($this->spinupwp->sites->list()); } @@ -24,7 +24,7 @@ protected function action() return $sites; } - return $sites->map(fn($site) => [ + return $sites->map(fn ($site) => [ 'ID' => $site->id, 'Server ID' => $site->server_id, 'Domain' => $site->domain, diff --git a/tests/Feature/Commands/ServersListCommandTest.php b/tests/Feature/Commands/ServersListCommandTest.php index 692d149..6f6ad05 100644 --- a/tests/Feature/Commands/ServersListCommandTest.php +++ b/tests/Feature/Commands/ServersListCommandTest.php @@ -15,8 +15,8 @@ 'used' => 7661140000, 'updated_at' => '2021-11-03T16:52:48.000000Z', ], - 'database' => [ - 'server' => 'mysql-8.0' + 'database' => [ + 'server' => 'mysql-8.0', ], ], [ @@ -31,8 +31,8 @@ 'used' => 7661140000, 'updated_at' => '2021-11-03T16:52:48.000000Z', ], - 'database' => [ - 'server' => 'mysql-8.0' + 'database' => [ + 'server' => 'mysql-8.0', ], ], ]; @@ -69,14 +69,14 @@ 'hellfish-media', '127.0.0.1', '20.04', - 'mysql-8.0' + 'mysql-8.0', ], [ '2', 'staging.hellfish-media', '127.0.0.1', '20.04', - 'mysql-8.0' + 'mysql-8.0', ], ] ); diff --git a/tests/Feature/Commands/SitesListCommandTest.php b/tests/Feature/Commands/SitesListCommandTest.php index 2961705..6def05f 100644 --- a/tests/Feature/Commands/SitesListCommandTest.php +++ b/tests/Feature/Commands/SitesListCommandTest.php @@ -9,12 +9,12 @@ 'domain' => 'hellfishmedia.com', 'site_user' => 'hellfish', 'php_version' => '8.0', - 'page_cache' => [ + 'page_cache' => [ 'enabled' => true, ], 'https' => [ 'enabled' => true, - ] + ], ], [ 'id' => 2, @@ -22,12 +22,12 @@ 'domain' => 'staging.hellfishmedia.com', 'site_user' => 'staging-hellfish', 'php_version' => '8.0', - 'page_cache' => [ + 'page_cache' => [ 'enabled' => false, ], 'https' => [ 'enabled' => false, - ] + ], ], ]; beforeEach(function () use ($response) {