Skip to content

Commit 6511034

Browse files
committed
✨ CLI
Signed-off-by: Bruno Meilick <[email protected]>
1 parent 8216af1 commit 6511034

File tree

109 files changed

+9577
-61
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+9577
-61
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ node_modules
1111
/tests/site/accounts
1212
/tests/site/cache
1313
/tests/site/sessions
14+
/tests/janitor-*.php
1415

1516
# files of Composer dependencies that are not needed for the plugin
1617
/vendor/**/.*
@@ -32,3 +33,4 @@ node_modules
3233
/vendor/**/tests/*
3334
/vendor/**/php4/*
3435
/vendor/getkirby/composer-installer
36+

README.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ Kirby 3 Plugin for running jobs like cleaning the cache from within the Panel, P
1818
1919
> *TIP 2:* It can also create logs of what it did.
2020
21+
> *TIP 3:* I can also be used as an CLI.
22+
2123
1. [Custom Jobs](https://github.com/bnomei/kirby3-janitor#custom-jobs)
2224
1. [Query Language](https://github.com/bnomei/kirby3-janitor#queries)
2325
1. [Reload Panel](https://github.com/bnomei/kirby3-janitor#reload-panel-view)
2426
1. [Copy to Clipboard](https://github.com/bnomei/kirby3-janitor#copy-to-clipboard)
2527
1. [Open URL](https://github.com/bnomei/kirby3-janitor#open-url)
2628
1. [Download File](https://github.com/bnomei/kirby3-janitor#download-file)
29+
1. [CLI](https://github.com/bnomei/kirby3-janitor#cli)
2730

2831
## Commerical Usage
2932

@@ -109,8 +112,6 @@ janitor_query:
109112
},
110113
```
111114

112-
113-
114115
## Panel Features
115116

116117
### Context page and data
@@ -181,6 +182,68 @@ janitor_query:
181182

182183
> ATTENTION: The download dialog will only appear at [same origin](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes) otherwise it will behave like opening an url.
183184

185+
## CLI
186+
187+
This plugin comes with an executable php script called `janitor` to use in the terminal/console. You can use it with an *alias* like this:
188+
189+
```
190+
cd your/project/folder/root
191+
alias janitor='php site/plugins/kirby3-janitor/janitor'
192+
janitor --help
193+
```
194+
195+
> TIP: Depending on your OS you might need to make the script executable with `chmod 0755 site/plugins/kirby3-janitor/janitor` first.
196+
197+
> TIP: If you are using a custom folder setup you need to tell the janitor where to get the kirby instance from like this `janitor -kirby /public myJob`. See **Arguments** below.
198+
199+
### CLI Arguments
200+
```
201+
janitor --help
202+
203+
Usage: janitor [-f format, --format format (default: label)] [-h, --help] [-k kirby, --kirby kirby (default: /)] [-l, --list] [-v, --verbose] [job]
204+
...
205+
```
206+
207+
### Kirby Instance Loader
208+
209+
The CLI needs to load the same Kirby Instance you website does. To achieve this the CLI attempts to create a special file named `janitor-{HASH}.php` based on your public `index.php`. It only comments out the `echo` statement.
210+
211+
### CLI Examples
212+
213+
Examples based on the test from this plugin:
214+
215+
**clean with dynamic progressbar**
216+
```
217+
janitor --verbose clean
218+
219+
Using Kirby instance from: {PROJECT}/janitor-7848cb3c7677f4ff109682b2d9cd9978d46f7de8.php
220+
======================================================================> 100%
221+
200
222+
```
223+
224+
**print as table**
225+
```
226+
janitor -format table whistle
227+
228+
----------------
229+
| status | 200 |
230+
----------------
231+
| label ||
232+
----------------
233+
```
234+
235+
**print as json and store as file**
236+
```
237+
janitor -format json heist | cat > heist-$(date +%s).json
238+
cat heist-1573147345.json
239+
240+
{
241+
"status": 200,
242+
"label": "7 Coins looted at Bank!"
243+
}
244+
245+
```
246+
184247
## Settings
185248
186249
| bnomei.janitor. | Default | Description |
@@ -221,6 +284,11 @@ wget https://devkit.bnomei.com/plugin-janitor/clean/e9fe51f94eadabf54dbf2fbbd571
221284
curl -s https://devkit.bnomei.com/plugin-janitor/clean/e9fe51f94eadabf54dbf2fbbd57188b9abee436e > /dev/null
222285
```
223286

287+
## Dependencies
288+
289+
- [Symfony Finder](https://symfony.com/doc/current/components/finder.html)
290+
- [CLIMate](https://github.com/thephpleague/climate)
291+
224292
## Disclaimer
225293

226294
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/bnomei/kirby3-janitor/issues/new).

classes/CleanCacheFilesJob.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ public function job(): array
1818
$removed = 0;
1919
$finder = new Finder();
2020
$finder->files()->name('*.cache')->in($dir);
21+
$count = iterator_count($finder);
22+
$climate = \Bnomei\Janitor::climate();
23+
$progress = null;
24+
if ($count && $climate) {
25+
$progress = $climate->progress()->total($count);
26+
}
2127
foreach ($finder as $cacheFile) {
2228
if (F::remove($cacheFile->getRealPath())) {
2329
$removed++;
30+
if ($progress && $climate) {
31+
$progress->current($removed);
32+
}
2433
}
2534
}
2635
return [

classes/FlushSessionFilesJob.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ public function job(): array
1818
$removed = 0;
1919
$finder = new Finder();
2020
$finder->files()->name('*.sess')->in($dir);
21+
$count = iterator_count($finder);
22+
$climate = \Bnomei\Janitor::climate();
23+
$progress = null;
24+
if ($count && $climate) {
25+
$progress = $climate->progress()->total($count);
26+
}
2127
foreach ($finder as $cacheFile) {
2228
if (F::remove($cacheFile->getRealPath())) {
2329
$removed++;
30+
if ($progress && $climate) {
31+
$progress->current($removed);
32+
}
2433
}
2534
}
2635
return [

classes/Janitor.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Kirby\Cms\Page;
1010
use Kirby\Toolkit\A;
1111
use Kirby\Toolkit\Str;
12+
use League\CLImate\CLImate;
1213

1314
final class Janitor
1415
{
@@ -94,6 +95,15 @@ public function job(string $name, array $data = []): array
9495
];
9596
}
9697

98+
/**
99+
* @return mixed
100+
*/
101+
public function listJobs()
102+
{
103+
// find in jobs config
104+
return array_keys($this->option('jobs'));
105+
}
106+
97107
/**
98108
* @param string $name
99109
* @return mixed|string
@@ -227,4 +237,21 @@ public static function isTrue($val, $return_null = false): bool
227237
$boolval = ($boolval === null && !$return_null ? false : $boolval);
228238
return $boolval;
229239
}
240+
241+
/**
242+
* @var \League\CLImate\CLImate
243+
*/
244+
private static $climate;
245+
246+
/**
247+
* @param CLImate|null $climate
248+
* @return CLImate|null
249+
*/
250+
public static function climate(?CLImate $climate = null): ?CLImate
251+
{
252+
if ($climate && !self::$climate) {
253+
self::$climate = $climate;
254+
}
255+
return self::$climate;
256+
}
230257
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby3-janitor",
33
"type": "kirby-plugin",
4-
"version": "2.1.0",
4+
"version": "2.2.0",
55
"license": "MIT",
66
"description": "Kirby 3 Plugin for running jobs like cleaning the cache from within the Panel, PHP code or a cronjob",
77
"authors": [
@@ -34,6 +34,7 @@
3434
"require": {
3535
"php": ">=7.2.0",
3636
"getkirby/composer-installer": "^1.1",
37+
"league/climate": "^3.5",
3738
"symfony/finder": "^4.3"
3839
},
3940
"require-dev": {

0 commit comments

Comments
 (0)