Skip to content

Commit 871585c

Browse files
authored
Merge pull request #92 from bnomei/development
v3 commands
2 parents 687ac91 + b1b0e08 commit 871585c

Some content is hidden

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

63 files changed

+6042
-2157
lines changed

README.md

Lines changed: 308 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,347 @@
77
[![Maintainability](https://flat.badgen.net/codeclimate/maintainability/bnomei/kirby3-janitor)](https://codeclimate.com/github/bnomei/kirby3-janitor)
88
[![Twitter](https://flat.badgen.net/badge/twitter/bnomei?color=66d9ef)](https://twitter.com/bnomei)
99

10-
Kirby 3 Plugin for running jobs.
10+
Kirby 3 Plugin for running commands.
1111

1212
- It is a Panel Button!
13-
- It has jobs build-in for cleaning the cache, sessions, create zip-backup, pre-generate thumbs, open URLs, refresh the current Panel page and more.
14-
- You can define your own jobs (call API hooks, play a game, hack a server, ...)
15-
- It can be triggered in your frontend code and with CRON.
16-
- It can also be used as a CLI with fancy output.
17-
- It can also create logs of what it did.
13+
- It has commands built-in for cleaning the cache, sessions, create zip-backup, pre-generate thumbs, open URLs, refresh the current Panel page and more.
14+
- You can define your own commands (call API hooks, play a game, hack a server, ...)
15+
- It can be triggered in your frontend code, with the official kirby CLI and a CRON.
1816

1917
## Install
2018

2119
Using composer:
2220

2321
```bash
24-
composer require bnomei/kirby3-janitor
22+
composer require getkirby/cli bnomei/kirby3-janitor
2523
```
2624

2725
Using git submodules:
2826

2927
```bash
28+
git submodule add https://github.com/getkirby/cli.git site/plugins/cli
3029
git submodule add https://github.com/bnomei/kirby3-janitor.git site/plugins/kirby3-janitor
3130
```
3231

33-
Using download & copy: download [the latest release](https://github.com/bnomei/kirby3-janitor/releases) and copy to `site/plugins`
32+
Using download & copy: download [the latest release of this plugin](https://github.com/bnomei/kirby3-janitor/releases) and [the latest release of the kirby cli](https://github.com/getkirby/cli/releases) then unzip and copy them to `site/plugins`
3433

35-
## Commerical Usage
34+
## Commercial Usage
3635

3736
> <br>
38-
><b>Support open source!</b><br><br>
37+
> <b>Support open source!</b><br><br>
3938
> This plugin is free but if you use it in a commercial project please consider to sponsor me or make a donation.<br>
4039
> If my work helped you to make some cash it seems fair to me that I might get a little reward as well, right?<br><br>
4140
> Be kind. Share a little. Thanks.<br><br>
4241
> &dash; Bruno<br>
43-
> &nbsp;
42+
> &nbsp;
4443
4544
| M | O | N | E | Y |
4645
|---|----|---|---|---|
4746
| [Github sponsor](https://github.com/sponsors/bnomei) | [Patreon](https://patreon.com/bnomei) | [Buy Me a Coffee](https://buymeacoff.ee/bnomei) | [Paypal dontation](https://www.paypal.me/bnomei/15) | [Hire me](mailto:[email protected]?subject=Kirby) |
4847

49-
## Wiki
48+
## Setup
5049

51-
Continue to the [Janitor Wiki](https://github.com/bnomei/kirby3-janitor/wiki) to read more on how to install, setup and use this plugin.
50+
### CLI Command
51+
52+
In any blueprint create a janitor field with your command, browse to that page in panel and press the button.
53+
54+
**site/blueprints/page/default.yml**
55+
```yml
56+
title: Default Page
57+
fields:
58+
call_my_command:
59+
type: janitor
60+
command: 'example --data test'
61+
label: Call `Example` Command
62+
```
63+
64+
Janitor will automatically fill in the current model.
65+
66+
- So for example if you press the panel button on a page you will have `--page` argument set.
67+
- If you call it on a user then `--user` arg will be set.
68+
- On a panel file view... `--file`.
69+
- And lastly `--site` will be automatically set when you had the button in the `site/blueprints/site.yml` blueprint.
70+
71+
Create a Kirby CLI command [via a custom plugin](https://getkirby.com/docs/reference/plugins/extensions/commands) or put them into `site/commands`.
72+
73+
**site/commands/example.php**
74+
```php
75+
<?php
76+
77+
use Bnomei\Janitor;
78+
use Kirby\CLI\CLI;
79+
80+
return [
81+
'description' => 'Example',
82+
'args' => [] + Janitor::ARGS, // page, file, user, site, data
83+
'command' => static function (CLI $cli): void {
84+
$page = page($cli->arg('page'));
85+
86+
// output for the command line
87+
defined('STDOUT') && $cli->success(
88+
$model->title() . ' ' . $cli->arg('data')
89+
);
90+
91+
// output for janitor
92+
janitor()->data($cli->arg('command'), [
93+
'status' => 200,
94+
'message' => $model->title() . ' ' . $cli->arg('data'),
95+
]);
96+
}
97+
];
98+
99+
```
100+
101+
### Callback
102+
103+
Instead of using a command you can also create a callback in a custom plugin options or any config file.
104+
105+
**site/config/config.php**
106+
```php
107+
<?php
108+
109+
return [
110+
'example' => function ($model, $data = null) {
111+
return [
112+
'status' => 200,
113+
'message' => $model->title() . ' ' . $data,
114+
];
115+
},
116+
// ... other options
117+
];
118+
```
119+
120+
The Janitor plugin has a special command `janitor:job` that you can use to trigger your callback.
121+
122+
**site/blueprints/page/default.yml**
123+
```yml
124+
title: Default Page
125+
fields:
126+
call_my_command:
127+
type: janitor
128+
command: 'janitor:job --key example --data test'
129+
label: Call `Example` Command
130+
```
131+
132+
The `$model` will match the model of whatever page, file, user or site object you pressed the button at.
133+
134+
> Why just a single model variable instead of one each for page, file, user and site? For one reason to make it work directly with any existing version 2 callbacks you might have already created and secondly because this why it is very easy to get the model that triggered the callback.
135+
136+
### Built in commands and examples
137+
138+
This plugin comes with a [few commands](https://github.com/bnomei/kirby3-janitor/tree/master/commands) you might like to use yourself and some [example commands](https://github.com/bnomei/kirby3-janitor/tree/master/tests/site/commands) used to showcase the various options the button has (like how to change the icon or open a URL in a new tab). Some commands can be used in both panel and terminal. Others are limited in their use to either one of them. In the terminal you can use `--help` argument to view the help for each command.
139+
140+
- `janitor:backupzip`, creates a backup zip
141+
- `janitor:cleancontent`, removes fields from content file that are not defined in your blueprints
142+
- `janitor:clipboard`, copies a defined value to your clipboard
143+
- `janitor:download`, triggers a download of an URL
144+
- `janitor:flush`, flush a cache by providing its name
145+
- `janitor:job`, run a callback
146+
- `janitor:maintenance`, toggle maintenance mode
147+
- `janitor:open`, triggers opening of an URL in panel
148+
- `janitor:pipe`, map input argument to output argument
149+
- `janitor:render`, render a certain page or all pages (to create thumb jobs)
150+
- `janitor:thumbs`, process thumb jobs of a certain page or all pages
151+
- `janitor:tinker`, run a REPL session in terminal
152+
153+
### Blueprint field options
154+
155+
The button you create with the `field: janitor` in your blueprint can be configured to do various things. Checkout the [example default.yml blueprint](https://github.com/bnomei/kirby3-janitor/blob/master/tests/site/blueprints/pages/default.yml) to familiarize yourself with how to use it.
156+
157+
- `autosave`, if `true` then save before pressing the button
158+
- `command`, command like you would enter it in terminal, with [query language support](https://getkirby.com/docs/guide/blueprints/query-language) and page/file/user/site/data arguments
159+
- `cooldown`, time in milliseconds the message is flashed on the button (default: 2000)
160+
- `error`, set message to show on all **non-200**-status returns with query language support
161+
- `icon`, set the [icon](https://getkirby.com/docs/reference/panel/icons) of the button
162+
- `intab`, if `true` then use in combination with the `open`-option to open an URL in a new tab
163+
- `label`, set label of the button
164+
- `progress`, set message to show while the button waits for the response, with query language support
165+
- `success`, set message to show on all **200**-status returns, with query language support
166+
- `unsaved`, if `false` then disable the button if panel view has unsaved content
167+
168+
### Janitor API options
169+
170+
In either the command or the callback you will be setting/returning data to the Janitor button via its api. Depending on what you return you can trigger various things to happen in the panel.
171+
172+
- `clipboard`, string to copy to clipboard
173+
- `download`, URL to start downloading
174+
- `error`, see `error`-field option
175+
- `icon`, see `icon`-field option
176+
- `label`, see `label`-field option
177+
- `message`, see `message`-field option
178+
- `open`, URL to open, use with `intab`-field option to open in a new tab
179+
- `reload`, if `true` will reload panel view once api call is received
180+
- `success`, see `success`-field option
181+
- `status`, return `200` for a **green** button flash, anything else for a **red** flash
182+
183+
### Examples
184+
185+
Again... check out the [built-in commands](https://github.com/bnomei/kirby3-janitor/tree/master/commands) and plugin [example commands](https://github.com/bnomei/kirby3-janitor/tree/master/tests/site/commands) to learn how to use the field and api options yourself.
186+
187+
```yml
188+
test_ping:
189+
type: janitor
190+
command: 'ping' # see tests/site/commands/ping.php
191+
label: Ping
192+
progress: ....
193+
success: Pong
194+
error: BAMM
195+
196+
janitor_open:
197+
type: janitor
198+
command: 'janitor:open --data {{ user.panel.url }}'
199+
intab: true
200+
label: Open current user URL in new tab
201+
icon: open
202+
# the open command will forward the `data` arg to `open` and open that URL
203+
204+
janitor_clipboarddata:
205+
type: janitor
206+
command: 'janitor:clipboard --data {{ page.title }}'
207+
label: 'Copy "{{ page.title }}" to Clipboard'
208+
progress: Copied!
209+
icon: copy
210+
# the clipboard command will forward the `data` arg to `clipboard` and copy that
211+
212+
janitor_download:
213+
type: janitor
214+
command: 'janitor:download --data {{ site.index.files.first.url }}'
215+
label: Download File Example
216+
icon: download
217+
# the download command will forward the `data` arg to `download` and start downloading that
218+
219+
janitor_backupzip:
220+
type: janitor
221+
command: 'janitor:backupzip'
222+
cooldown: 5000
223+
label: Generate Backup ZIP
224+
icon: archive
225+
226+
janitor_render:
227+
type: janitor
228+
command: 'janitor:render'
229+
label: Render pages to create missing thumb jobs
230+
231+
janitor_thumbssite:
232+
type: janitor
233+
command: 'janitor:thumbs --site'
234+
label: Generate thumbs from existing thumb jobs (full site)
235+
```
236+
237+
If you want you can also call any of [the core shipping with the CLI](https://github.com/getkirby/cli#available-core-commands) like `clear:cache`.
238+
239+
### Running commands in your code
240+
241+
You can run any command in you own code as well like in a model, template, controller or hook. Since commands do not return data directly you need to retrieve data stored for Janitor using a helper `janitor()->data($commandName)`.
242+
243+
#### Get data returned from a command
244+
```php
245+
Kirby\CLI\CLI::command('whistle'); // tests/site/commands/whistle.php
246+
var_dump(janitor()->data('whistle'));
247+
```
248+
249+
#### Create and download a backup
250+
```php
251+
Kirby\CLI\CLI::command('janitor:backupzip');
252+
$backup = janitor()->data('janitor:backupzip')['path'];
253+
if(F::exists($backup)) {
254+
Header::download([
255+
'mime' => F::mime($backup),
256+
'name' => F::filename($backup),
257+
]);
258+
readfile($backup);
259+
die(); // needed to make content type work
260+
}
261+
```
262+
263+
#### Calling a command with parameters
264+
265+
Supplying parameter to the core CLI functions can be a bit tricky since you need to separate argument key and argument values. It seems easy with one but gets a bit tedious with a dynamic list of parameters and if values contain `space`-chars or quotes. But fret not – Janitor has a helper for that as well.
266+
267+
```php
268+
Kirby\CLI\CLI::command('uuid', '--page', 'some/page'); // tests/site/commands/uuid.php
269+
270+
janitor()->command('uuid --page some/page');
271+
272+
var_dump(janitor()->data('uuid')['message']); // page://82h2nkal12ls
273+
```
274+
275+
> Remember that using the `janitor()->command($string)`-helper you can call any of your own commands and the core commands as well, not just the ones defined by Janitor.
276+
277+
If you want to work with command strings yourself you can use the following static helper method.
278+
279+
```php
280+
list($name, $args) = Bnomei\Janitor::parseCommand('uuid --page page://82h2nkal12ls');
281+
Kirby\CLI\CLI::command($name, ...$args);
282+
```
283+
284+
285+
### Webhook with secret
286+
287+
You can not call Janitors api unauthenticated. You either need to use the panel button or you can set a `secret` in your `site/config/config.php` file and call the janitor api URL with that secret.
288+
289+
**site/config/config.php**
290+
```php
291+
<?php
292+
293+
return [
294+
'bnomei.janitor.secret' => 'e9fe51f94eadabf54', // whatever string you like
295+
//... other options
296+
];
297+
```
298+
299+
You could also use a callback if you want to store the secret in a `.env` file and have it loaded by my [dotenv plugin](https://github.com/bnomei/kirby3-dotenv).
300+
301+
**/.env**
302+
```dotenv
303+
# whatever key and value you like
304+
MY_JANITOR_SECRET=e9fe51f94eadabf54
305+
```
306+
307+
**site/config/config.php**
308+
```php
309+
<?php
310+
311+
return [
312+
'bnomei.janitor.secret' => fn() => env('MY_JANITOR_SECRET'),
313+
//... other options
314+
];
315+
```
316+
317+
#### example URL
318+
```
319+
https://dev.bnomei.com/plugin-janitor/e9fe51f94eadabf54/janitor%3Abackupzip
320+
```
321+
322+
#### example URL with urlencoded arguments
323+
```
324+
http://dev.bnomei.com/plugin-janitor/e9fe51f94eadabf54/janitor%3Athumbs%20--site
325+
```
326+
### CRON
327+
328+
#### Webhook with wget or curl
329+
330+
You can also use the secret to trigger a job using wget or curl.
331+
332+
```php
333+
wget https://dev.bnomei.com/plugin-janitor/e9fe51f94eadabf54/janitor%3Abackupzip --delete-after
334+
// or
335+
curl -s https://dev.bnomei.com/plugin-janitor/e9fe51f94eadabf54/janitor%3Abackupzip > /dev/null
336+
```
337+
338+
#### Kirby CLI (installed with composer)
339+
340+
in your cron scheduler add the following command
341+
342+
```
343+
cd /path/to/my/kirby/project/root && vendor/bin/kirby janitor:backupzip
344+
```
52345

53346
## Dependencies
54347

348+
- [Kirby CLI](https://github.com/getkirby/cli)
349+
- [CLImate](https://github.com/thephpleague/climate)
55350
- [Symfony Finder](https://symfony.com/doc/current/components/finder.html)
56-
- [CLIMate](https://github.com/thephpleague/climate)
57351

58352
## Disclaimer
59353

0 commit comments

Comments
 (0)