-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathClearLogs.php
More file actions
73 lines (57 loc) · 1.86 KB
/
ClearLogs.php
File metadata and controls
73 lines (57 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Housekeeping;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;
/**
* Clears all log files.
*/
#[Command(name: 'logs:clear', description: 'Clears all log files.', group: 'Housekeeping')]
class ClearLogs extends AbstractCommand
{
protected function configure(): void
{
$this->addOption(new Option(
name: 'force',
shortcut: 'f',
description: 'Forces the clearing of log files without confirmation.',
));
}
protected function interact(array &$arguments, array &$options): void
{
if ($this->hasUnboundOption('force', $options)) {
return;
}
if (CLI::prompt('Are you sure you want to delete the logs?', ['n', 'y']) === 'n') {
return;
}
$options['force'] = null; // simulate the presence of the --force option
}
protected function execute(array $arguments, array $options): int
{
if ($options['force'] === false) {
CLI::error('Deleting logs aborted.');
if (! $this->isInteractive()) {
CLI::error('If you want, use the "--force" option to force delete all log files.');
}
return EXIT_ERROR;
}
helper('filesystem');
if (! delete_files(WRITEPATH . 'logs', htdocs: true)) {
CLI::error('Error in deleting the logs files.');
return EXIT_ERROR;
}
CLI::write('Logs cleared.', 'green');
return EXIT_SUCCESS;
}
}