diff --git a/app/Commands/BaseCommand.php b/app/Commands/BaseCommand.php index 57a5279..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 ? 'yes' : 'no'; + $value = $value ? 'Y' : 'N'; } return $value; }, array_values($item)); diff --git a/app/Commands/Servers/ListCommand.php b/app/Commands/Servers/ListCommand.php index 5abccaf..349d7dd 100644 --- a/app/Commands/Servers/ListCommand.php +++ b/app/Commands/Servers/ListCommand.php @@ -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'], ]); } } diff --git a/app/Commands/Sites/ListCommand.php b/app/Commands/Sites/ListCommand.php new file mode 100644 index 0000000..99552a9 --- /dev/null +++ b/app/Commands/Sites/ListCommand.php @@ -0,0 +1,37 @@ +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'], + ]); + } +} diff --git a/tests/Feature/Commands/ServersListCommandTest.php b/tests/Feature/Commands/ServersListCommandTest.php index 2a38dbb..6f6ad05 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', ], ] ); diff --git a/tests/Feature/Commands/SitesListCommandTest.php b/tests/Feature/Commands/SitesListCommandTest.php new file mode 100644 index 0000000..6def05f --- /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', + ], + ] + ); +});