lasthourcloud/app/Console/Commands/ServicesDelete.php

148 lines
4.5 KiB
PHP
Raw Normal View History

2023-10-03 09:56:56 +00:00
<?php
namespace App\Console\Commands;
2024-01-12 08:11:36 +00:00
use App\Jobs\DeleteResourceJob;
2023-10-03 09:56:56 +00:00
use App\Models\Application;
2023-10-11 12:04:21 +00:00
use App\Models\Server;
2023-10-03 09:56:56 +00:00
use App\Models\Service;
use App\Models\StandalonePostgresql;
use Illuminate\Console\Command;
use function Laravel\Prompts\confirm;
2023-10-04 12:40:04 +00:00
use function Laravel\Prompts\multiselect;
2023-10-03 09:56:56 +00:00
use function Laravel\Prompts\select;
class ServicesDelete extends Command
2023-10-03 09:56:56 +00:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'services:delete';
2023-10-03 09:56:56 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete a service from the database';
2023-10-03 09:56:56 +00:00
/**
* Execute the console command.
*/
public function handle()
{
$resource = select(
'What service do you want to delete?',
2023-10-11 12:04:21 +00:00
['Application', 'Database', 'Service', 'Server'],
2023-10-03 09:56:56 +00:00
);
if ($resource === 'Application') {
$this->deleteApplication();
} elseif ($resource === 'Database') {
$this->deleteDatabase();
} elseif ($resource === 'Service') {
$this->deleteService();
2023-10-12 11:35:57 +00:00
} elseif ($resource === 'Server') {
2023-10-11 12:04:21 +00:00
$this->deleteServer();
}
}
2023-10-12 11:35:57 +00:00
private function deleteServer()
{
2023-10-11 12:04:21 +00:00
$servers = Server::all();
if ($servers->count() === 0) {
$this->error('There are no applications to delete.');
return;
}
$serversToDelete = multiselect(
2023-10-12 11:35:57 +00:00
label: 'What server do you want to delete?',
options: $servers->pluck('name', 'id')->sortKeys(),
2023-10-11 12:04:21 +00:00
);
2023-10-11 12:12:02 +00:00
2023-10-12 11:35:57 +00:00
foreach ($serversToDelete as $server) {
$toDelete = $servers->where('id', $server)->first();
2023-11-06 09:55:46 +00:00
if ($toDelete) {
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
break;
}
$toDelete->delete();
2023-10-11 12:12:02 +00:00
}
2023-10-03 09:56:56 +00:00
}
}
2023-10-03 09:59:30 +00:00
private function deleteApplication()
{
2023-10-03 09:56:56 +00:00
$applications = Application::all();
2023-10-03 10:08:57 +00:00
if ($applications->count() === 0) {
$this->error('There are no applications to delete.');
return;
}
2023-10-04 12:40:04 +00:00
$applicationsToDelete = multiselect(
2023-10-03 09:56:56 +00:00
'What application do you want to delete?',
2023-10-12 11:35:57 +00:00
$applications->pluck('name', 'id')->sortKeys(),
2023-10-03 09:56:56 +00:00
);
2023-10-11 12:12:02 +00:00
2023-10-04 12:40:04 +00:00
foreach ($applicationsToDelete as $application) {
2023-10-12 11:35:57 +00:00
$toDelete = $applications->where('id', $application)->first();
2023-11-06 09:55:46 +00:00
if ($toDelete) {
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources? ");
if (!$confirmed) {
break;
}
2024-01-12 08:11:36 +00:00
DeleteResourceJob::dispatch($toDelete);
2023-10-11 12:12:02 +00:00
}
2023-10-04 12:40:04 +00:00
}
2023-10-03 09:56:56 +00:00
}
2023-10-03 09:59:30 +00:00
private function deleteDatabase()
{
2023-10-03 09:56:56 +00:00
$databases = StandalonePostgresql::all();
2023-10-03 10:08:57 +00:00
if ($databases->count() === 0) {
$this->error('There are no databases to delete.');
return;
}
2023-10-04 12:40:04 +00:00
$databasesToDelete = multiselect(
2023-10-03 09:56:56 +00:00
'What database do you want to delete?',
2023-10-12 11:35:57 +00:00
$databases->pluck('name', 'id')->sortKeys(),
2023-10-03 09:56:56 +00:00
);
2023-10-11 12:12:02 +00:00
2023-10-04 12:40:04 +00:00
foreach ($databasesToDelete as $database) {
2023-10-12 11:35:57 +00:00
$toDelete = $databases->where('id', $database)->first();
2023-11-06 09:55:46 +00:00
if ($toDelete) {
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
return;
}
2024-01-12 08:11:36 +00:00
DeleteResourceJob::dispatch($toDelete);
2023-10-11 12:12:02 +00:00
}
2023-10-04 12:40:04 +00:00
}
2023-10-03 09:56:56 +00:00
}
2023-10-03 09:59:30 +00:00
private function deleteService()
{
2023-10-03 09:56:56 +00:00
$services = Service::all();
2023-10-03 10:08:57 +00:00
if ($services->count() === 0) {
$this->error('There are no services to delete.');
return;
}
2023-10-04 12:40:04 +00:00
$servicesToDelete = multiselect(
2023-10-03 09:56:56 +00:00
'What service do you want to delete?',
2023-10-12 11:35:57 +00:00
$services->pluck('name', 'id')->sortKeys(),
2023-10-03 09:56:56 +00:00
);
2023-10-11 12:12:02 +00:00
2023-10-04 12:40:04 +00:00
foreach ($servicesToDelete as $service) {
2023-10-12 11:35:57 +00:00
$toDelete = $services->where('id', $service)->first();
2023-11-06 09:55:46 +00:00
if ($toDelete) {
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
return;
}
2024-01-12 08:11:36 +00:00
DeleteResourceJob::dispatch($toDelete);
2023-10-11 12:12:02 +00:00
}
2023-10-04 12:40:04 +00:00
}
2023-10-03 09:56:56 +00:00
}
}