-
Notifications
You must be signed in to change notification settings - Fork 33
Added the McLogCleaner-Plugin #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JuggleGaming
wants to merge
20
commits into
pelican-dev:main
Choose a base branch
from
JuggleGaming:mclogcleaner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−0
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
452d118
Added the McLogCleaner-Plugin
JuggleGaming e664ed0
Fixed some things coderrabbitai wanted me to change :D
JuggleGaming 5b82b62
Fixed some things coderrabbitai wanted me to change :D
JuggleGaming 02820d0
Fixed some things coderrabbitai wanted me to change :D
JuggleGaming 6d2fccb
Trying to fix things coderabitai want me to do
JuggleGaming 6ccf949
Trying to fix things coderabitai want me to do
JuggleGaming e605074
Trying to fix things coderabitai want me to do
JuggleGaming 1d9c20b
Fixed typos
JuggleGaming c191534
Fixed an issue seen by coderabbitai
JuggleGaming 4d4bafe
Fixed an issue seen by coderabbitai
JuggleGaming 4356a8a
Fixed an issue seen by coderabbitai
JuggleGaming 82b6f28
Fixed an issue seen by coderabbitai
JuggleGaming 926ada5
Fixed an issue seen by coderabbitai
JuggleGaming 70b70bf
Fixed an issue seen by coderabbitai
JuggleGaming b49d4ab
Fixed an issue seen by coderabbitai
JuggleGaming 5cca94e
Updated version to 1.1.1 to fit to my repo (and the version in discord)
JuggleGaming c9a35e9
Deleted unnecessary config an enum. The feature-check is now inside o…
JuggleGaming 5828281
Fix
JuggleGaming 3fcc4d8
Fix
JuggleGaming 9b0a549
Fix
JuggleGaming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # McLogCleaner (by JuggleGaming) | ||
|
|
||
| McLogCleaner automatically deletes all `.log.gz` files from the server’s `logs` folder. | ||
|
|
||
| > **Note:** `latest.log` will always remain intact and is never deleted. | ||
|
|
||
| ## Setup | ||
| To use this plugin, add `mclogcleaner` as a _feature_ to the egg you want to run it with. | ||
|
|
||
| ## Log Deletion Options | ||
| When you click **Delete logs**, a dropdown menu appears where you can choose the **minimum age (in days)** of log files to delete: | ||
| - Logs older than 7 days | ||
| - Logs older than 30 days | ||
| - All logs (regardless of age) | ||
| - A custom age in days |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "id": "mclogcleaner", | ||
| "name": "McLogCleaner", | ||
| "author": "JuggleGaming", | ||
| "version": "1.1.1", | ||
| "description": "Clean your Minecraft-logs with ease", | ||
| "category": "plugin", | ||
| "url": "https://github.com/pelican-dev/plugins/tree/main/mclogcleaner", | ||
| "update_url": null, | ||
| "namespace": "JuggleGaming\\McLogCleaner", | ||
| "class": "McLogCleanerPlugin", | ||
| "panels": null, | ||
| "panel_version": null, | ||
| "composer_packages": null | ||
| } |
151 changes: 151 additions & 0 deletions
151
mclogcleaner/src/Filament/Components/Actions/McLogCleanAction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| <?php | ||
|
|
||
| namespace JuggleGaming\McLogCleaner\Filament\Components\Actions; | ||
|
|
||
| use App\Models\Server; | ||
| use Carbon\Carbon; | ||
| use Exception; | ||
| use Filament\Actions\Action; | ||
| use Filament\Facades\Filament; | ||
| use Filament\Forms\Components\Select; | ||
| use Filament\Forms\Components\TextInput; | ||
| use Filament\Notifications\Notification; | ||
| use Filament\Support\Enums\Size; | ||
| use Illuminate\Support\Facades\Http; | ||
|
|
||
| class McLogCleanAction extends Action | ||
| { | ||
| public static function getDefaultName(): ?string | ||
| { | ||
| return 'clean_logs'; | ||
| } | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->hidden(function () { | ||
| /** @var Server|null $server */ | ||
| $server = Filament::getTenant(); | ||
| if (!$server) { | ||
| return true; | ||
| } | ||
| $server->loadMissing('egg'); | ||
| $features = $server->egg->features ?? []; | ||
|
|
||
| return !in_array('mclogcleaner', $features, true); | ||
| }); | ||
|
|
||
| $this->label('Delete logs'); | ||
| $this->icon('tabler-trash'); | ||
| $this->color('danger'); | ||
| $this->size(Size::ExtraLarge); | ||
| $this->requiresConfirmation() | ||
| ->modalHeading('Delete logs') | ||
| ->modalDescription('Choose which logs should be deleted.') | ||
| ->modalSubmitActionLabel('Delete logs') | ||
| ->form([ | ||
| Select::make('mode') | ||
| ->label('Delete logs') | ||
| ->options([ | ||
| 7 => 'Older than 7 days', | ||
| 30 => 'Older than 30 days', | ||
| -1 => 'Delete all logs', | ||
| 'custom' => 'Custom (days)', | ||
| ]) | ||
| ->default(7) | ||
| ->required() | ||
| ->reactive(), | ||
| TextInput::make('custom_days') | ||
| ->label('Delete logs older than (days)') | ||
| ->numeric() | ||
| ->minValue(1) | ||
| ->maxValue(365) | ||
| ->placeholder('e.g. 14') | ||
| ->required(fn ($get) => $get('mode') === 'custom') | ||
| ->visible(fn ($get) => $get('mode') === 'custom'), | ||
| ]); | ||
|
|
||
| $this->action(function (array $data) { | ||
| /** @var Server|null $server */ | ||
| $server = Filament::getTenant(); | ||
| $mode = $data['mode']; | ||
| if ($mode !== 'custom') { | ||
| $mode = (int) $mode; | ||
| } | ||
| if ($mode === 'custom') { | ||
| $days = max(1, (int) $data['custom_days']); | ||
| } elseif ($mode === -1) { | ||
| $days = 0; | ||
| } else { | ||
| $days = $mode; | ||
| } | ||
| try { | ||
| $files = Http::daemon($server->node) | ||
| ->get("/api/servers/{$server->uuid}/files/list-directory", [ | ||
| 'directory' => 'logs', | ||
| ]) | ||
| ->throw() | ||
| ->json(); | ||
| if (!is_array($files)) { | ||
| throw new Exception('Invalid log directory response.'); | ||
| } | ||
| $threshold = now()->subDays($days)->startOfDay(); | ||
| $logsToDelete = collect($files) | ||
| ->filter(fn ($file) => str_ends_with($file['name'], '.log.gz')) | ||
| ->filter(function ($file) use ($days, $threshold) { | ||
| if ($days === 0) { | ||
| return true; | ||
| } | ||
| $logDate = $this->extractLogDate($file['name']); | ||
| if (!$logDate) { | ||
| return false; | ||
| } | ||
|
|
||
| return $logDate->lessThan($threshold); | ||
| }) | ||
| ->pluck('name') | ||
| ->map(fn ($name) => 'logs/' . $name) | ||
| ->values() | ||
| ->all(); | ||
| if (empty($logsToDelete)) { | ||
| Notification::make() | ||
| ->title('McLogCleaner') | ||
| ->body('No logs matching your selection were found.') | ||
| ->success() | ||
| ->send(); | ||
|
|
||
| return; | ||
| } | ||
| Http::daemon($server->node) | ||
| ->post("/api/servers/{$server->uuid}/files/delete", [ | ||
| 'root' => '/', | ||
| 'files' => $logsToDelete, | ||
| ]) | ||
| ->throw(); | ||
| Notification::make() | ||
| ->title('Logfolder cleaned') | ||
| ->body(count($logsToDelete) . ' files were deleted.') | ||
| ->success() | ||
| ->send(); | ||
| } catch (\Throwable $e) { | ||
| report($e); | ||
| Notification::make() | ||
| ->title('Cleanup failed.') | ||
| ->body('An error occurred while deleting log files. Please try again later.') | ||
| ->danger() | ||
| ->send(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private function extractLogDate(string $filename): ?Carbon | ||
| { | ||
| if (preg_match('/(\d{4}-\d{2}-\d{2})/', $filename, $matches)) { | ||
| $date = Carbon::createFromFormat('Y-m-d', $matches[1]); | ||
|
|
||
| return $date ? $date->startOfDay() : null; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace JuggleGaming\McLogCleaner; | ||
|
|
||
| use Filament\Contracts\Plugin; | ||
| use Filament\Panel; | ||
|
|
||
| class McLogCleanerPlugin implements Plugin | ||
| { | ||
| public function getId(): string | ||
| { | ||
| return 'mclogcleaner'; | ||
| } | ||
|
|
||
| public function register(Panel $panel): void | ||
| { | ||
| // | ||
| } | ||
|
|
||
| public function boot(Panel $panel): void | ||
| { | ||
| // | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace JuggleGaming\McLogCleaner\Providers; | ||
|
|
||
| use App\Enums\HeaderActionPosition; | ||
| use App\Filament\Server\Pages\Console; | ||
| use Illuminate\Support\ServiceProvider; | ||
| use JuggleGaming\McLogCleaner\Filament\Components\Actions\McLogCleanAction; | ||
|
|
||
| class McLogCleanerPluginProvider extends ServiceProvider | ||
| { | ||
| public function register(): void | ||
| { | ||
| Console::registerCustomHeaderActions(HeaderActionPosition::Before, McLogCleanAction::make()); | ||
| } | ||
|
|
||
| public function boot(): void | ||
| { | ||
| // | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.