coolify/app/Console/Commands/ResourcesDelete.php

139 lines
4.1 KiB
PHP
Raw Normal View History

2023-10-03 11:56:56 +02:00
<?php
namespace App\Console\Commands;
use App\Models\Application;
2023-10-11 14:04:21 +02:00
use App\Models\Server;
2023-10-03 11:56:56 +02:00
use App\Models\Service;
use App\Models\StandalonePostgresql;
use Illuminate\Console\Command;
use function Laravel\Prompts\confirm;
2023-10-04 14:40:04 +02:00
use function Laravel\Prompts\multiselect;
2023-10-03 11:56:56 +02:00
use function Laravel\Prompts\select;
2023-10-03 12:08:57 +02:00
class ResourcesDelete extends Command
2023-10-03 11:56:56 +02:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2023-10-03 12:08:57 +02:00
protected $signature = 'resources:delete';
2023-10-03 11:56:56 +02: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 14:04:21 +02:00
['Application', 'Database', 'Service', 'Server'],
2023-10-03 11:56:56 +02:00
);
if ($resource === 'Application') {
$this->deleteApplication();
} elseif ($resource === 'Database') {
$this->deleteDatabase();
} elseif ($resource === 'Service') {
$this->deleteService();
2023-10-11 14:04:21 +02: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 14:12:02 +02:00
$servers->pluck('id')->sort()->toArray(),
2023-10-11 14:04:21 +02:00
);
2023-10-11 14:12:02 +02:00
2023-10-11 15:39:27 +02:00
foreach ($serversToDelete as $id) {
$toDelete = $servers->find($id)->first();
2023-10-11 14:12:02 +02:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
break;
}
2023-10-11 14:04:21 +02:00
$toDelete->delete();
2023-10-03 11:56:56 +02:00
}
}
2023-10-03 11:59:30 +02:00
private function deleteApplication()
{
2023-10-03 11:56:56 +02:00
$applications = Application::all();
2023-10-03 12:08:57 +02:00
if ($applications->count() === 0) {
$this->error('There are no applications to delete.');
return;
}
2023-10-04 14:40:04 +02:00
$applicationsToDelete = multiselect(
2023-10-03 11:56:56 +02:00
'What application do you want to delete?',
2023-10-11 14:12:02 +02:00
$applications->pluck('name')->sort()->toArray(),
2023-10-03 11:56:56 +02:00
);
2023-10-11 14:12:02 +02:00
2023-10-04 14:40:04 +02:00
foreach ($applicationsToDelete as $application) {
$toDelete = $applications->where('name', $application)->first();
2023-10-11 14:12:02 +02:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources? ");
if (!$confirmed) {
break;
}
2023-10-04 14:40:04 +02:00
$toDelete->delete();
}
2023-10-03 11:56:56 +02:00
}
2023-10-03 11:59:30 +02:00
private function deleteDatabase()
{
2023-10-03 11:56:56 +02:00
$databases = StandalonePostgresql::all();
2023-10-03 12:08:57 +02:00
if ($databases->count() === 0) {
$this->error('There are no databases to delete.');
return;
}
2023-10-04 14:40:04 +02:00
$databasesToDelete = multiselect(
2023-10-03 11:56:56 +02:00
'What database do you want to delete?',
2023-10-11 14:12:02 +02:00
$databases->pluck('name')->sort()->toArray(),
2023-10-03 11:56:56 +02:00
);
2023-10-11 14:12:02 +02:00
2023-10-04 14:40:04 +02:00
foreach ($databasesToDelete as $database) {
$toDelete = $databases->where('name', $database)->first();
2023-10-11 14:12:02 +02:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
return;
}
2023-10-04 14:40:04 +02:00
$toDelete->delete();
}
2023-10-03 11:56:56 +02:00
}
2023-10-03 11:59:30 +02:00
private function deleteService()
{
2023-10-03 11:56:56 +02:00
$services = Service::all();
2023-10-03 12:08:57 +02:00
if ($services->count() === 0) {
$this->error('There are no services to delete.');
return;
}
2023-10-04 14:40:04 +02:00
$servicesToDelete = multiselect(
2023-10-03 11:56:56 +02:00
'What service do you want to delete?',
2023-10-11 14:12:02 +02:00
$services->pluck('name')->sort()->toArray(),
2023-10-03 11:56:56 +02:00
);
2023-10-11 14:12:02 +02:00
2023-10-04 14:40:04 +02:00
foreach ($servicesToDelete as $service) {
$toDelete = $services->where('name', $service)->first();
2023-10-11 14:12:02 +02:00
$this->info($toDelete);
$confirmed = confirm("Are you sure you want to delete all selected resources?");
if (!$confirmed) {
return;
}
2023-10-04 14:40:04 +02:00
$toDelete->delete();
}
2023-10-03 11:56:56 +02:00
}
}