lasthourcloud/app/Console/Commands/ResourcesDelete.php

139 lines
4.1 KiB
PHP
Raw Normal View History

2023-10-03 09:56:56 +00:00
<?php
namespace App\Console\Commands;
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;
2023-10-03 10:08:57 +00:00
class ResourcesDelete extends Command
2023-10-03 09:56:56 +00:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2023-10-03 10:08:57 +00:00
protected $signature = 'resources:delete';
2023-10-03 09:56:56 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete a resource from the database';
/**
* Execute the console command.
*/
public function handle()
{
$resource = select(
'What resource 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-11 12:04:21 +00:00
} elseif($resource === 'Server') {
$this->deleteServer();
}
}
private function deleteServer() {
$servers = Server::all();
if ($servers->count() === 0) {
$this->error('There are no applications to delete.');
return;
}
$serversToDelete = multiselect(
'What server do you want to delete?',
2023-10-11 12:12:02 +00:00
$servers->pluck('id')->sort()->toArray(),
2023-10-11 12:04:21 +00:00
);
2023-10-11 12:12:02 +00:00
2023-10-11 13:39:27 +00:00
foreach ($serversToDelete as $id) {
2023-10-11 13:46:59 +00:00
$toDelete = Server::find($id);
2023-10-11 12:12:02 +00:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
break;
}
2023-10-11 12:04:21 +00:00
$toDelete->delete();
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-11 12:12:02 +00:00
$applications->pluck('name')->sort()->toArray(),
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) {
$toDelete = $applications->where('name', $application)->first();
2023-10-11 12:12:02 +00:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources? ");
if (!$confirmed) {
break;
}
2023-10-04 12:40:04 +00:00
$toDelete->delete();
}
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-11 12:12:02 +00:00
$databases->pluck('name')->sort()->toArray(),
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) {
$toDelete = $databases->where('name', $database)->first();
2023-10-11 12:12:02 +00:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
return;
}
2023-10-04 12:40:04 +00:00
$toDelete->delete();
}
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-11 12:12:02 +00:00
$services->pluck('name')->sort()->toArray(),
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) {
$toDelete = $services->where('name', $service)->first();
2023-10-11 12:12:02 +00:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
return;
}
2023-10-04 12:40:04 +00:00
$toDelete->delete();
}
2023-10-03 09:56:56 +00:00
}
}