Skip to content

Commit

Permalink
Merge pull request #76 from getkirby/develop
Browse files Browse the repository at this point in the history
Kirby CLI 1.4.0
  • Loading branch information
bastianallgeier authored Feb 29, 2024
2 parents 1b7d168 + 276f277 commit 8cb3477
Show file tree
Hide file tree
Showing 16 changed files with 527 additions and 45 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ This should print the Kirby CLI version and a list of available commands
## Available core commands

```
- kirby backup
- kirby clean:content
- kirby clear:cache
- kirby clear:lock
- kirby clear:logins
- kirby clear:media
- kirby clear:sessions
- kirby download
Expand All @@ -51,14 +54,20 @@ This should print the Kirby CLI version and a list of available commands
- kirby make:command
- kirby make:config
- kirby make:controller
- kirby make:language
- kirby make:model
- kirby make:plugin
- kirby make:snippet
- kirby make:template
- kirby make:user
- kirby plugin:install
- kirby plugin:remove
- kirby plugin:upgrade
- kirby register
- kirby remove:command
- kirby roots
- kirby unzip
- kirby upgrade
- kirby uuid:generate
- kirby uuid:populate
- kirby uuid:remove
Expand Down Expand Up @@ -137,6 +146,14 @@ kirby remove:command hello

If you have a local and a global command, you can choose which one to delete.

## Debugging

Use the `-d` or `--debug` argument to run the command in debug mode:

```bash
kirby make:command hello --debug
```

## Formatting Output

Sending messages to the terminal is super easy.
Expand Down Expand Up @@ -282,8 +299,3 @@ return [

© 2009 Bastian Allgeier
[getkirby.com](https://getkirby.com) · [License agreement](./LICENSE.md)





81 changes: 81 additions & 0 deletions commands/backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\Dir;

return [
'description' => 'Creates backup of application files',
'args' => [
'root' => [
'description' => 'Selects the kirby root, which should be backuped'
]
],
'command' => static function (CLI $cli): void {
if (class_exists('ZipArchive') === false) {
throw new Exception('ZipArchive library could not be found');
}

$root = $cli->argOrPrompt(
'root',
'Which root should be backuped? (press <Enter> to backup your entire site)',
false
);

$root = empty($root) === true ? 'index' : $root;
$rootPath = $cli->kirby()->root($root);

if ($rootPath === null) {
throw new Exception('Invalid root entered: ' . $root);
}

if (is_dir($rootPath) === false) {
throw new Exception('The root does not exist: ' . $root);
}

$kirbyPath = $cli->kirby()->root('index');
$backupPath = $kirbyPath . '/backup';
$backupFile = $backupPath . '/' . $root . '-' . date('Y-m-d-His') . '.zip';

if (is_file($backupFile) === true) {
throw new Exception('The backup file exists');
}

// create backup directory before the process
Dir::make($backupPath);

$zip = new ZipArchive();
if ($zip->open($backupFile, ZipArchive::CREATE) !== true) {
throw new Exception('Failed to create backup file');
}

$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$rootPath,
FilesystemIterator::SKIP_DOTS
),
fn ($file) => $file->isFile() || in_array($file->getBaseName(), ['.git', 'backup']) === false
)
);

foreach ($files as $file) {
// skip directories, will be added automatically
if ($file->isDir() === false) {
// get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);

// add current file to archive
$zip->addFile($filePath, $relativePath);
}
}

if ($zip->close() === false) {
throw new Exception('There was a problem writing the backup file');
}

$cli->success('The backup has been created: ' . substr($backupFile, strlen($kirbyPath)));
}
];
25 changes: 12 additions & 13 deletions commands/clean/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Kirby\CLI\CLI;

function clean(
$cleanContent = function (
Generator $collection,
array|null $ignore = null,
string|null $lang = null
Expand Down Expand Up @@ -36,18 +36,14 @@ function clean(
$data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted));

// try to update the page with the data
try {
$item->update($data, $lang);
} catch (Exception $e) {
throw $e->getMessage();
}
$item->update($data, $lang);
}
}
}
};

return [
'description' => 'Deletes all fields from page, file or user content files that are not defined in the blueprint, no matter if they contain content or not.',
'command' => static function (CLI $cli): void {
'command' => static function (CLI $cli) use ($cleanContent): void {

$cli->confirmToContinue('This will delete all fields from content files that are not defined in blueprints, no matter if they contain content or not. Are you sure?');

Expand All @@ -56,9 +52,6 @@ function clean(
// Authenticate as almighty
$kirby->impersonate('kirby');

// Define your collection
$collection = $kirby->models();

// set the fields to be ignored
$ignore = ['uuid', 'title', 'slug', 'template', 'sort', 'focus'];

Expand All @@ -67,11 +60,17 @@ function clean(
$languages = $kirby->languages();

foreach ($languages as $language) {
clean($collection, $ignore, $language->code());
// should call kirby models for each loop
// since generators cannot be cloned
// otherwise run into an exception
$collection = $kirby->models();

$cleanContent($collection, $ignore, $language->code());
}

} else {
clean($collection, $ignore);
$collection = $kirby->models();
$cleanContent($collection, $ignore);
}

$cli->success('The content files have been cleaned');
Expand Down
25 changes: 25 additions & 0 deletions commands/clear/lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\F;

return [
'description' => 'Deletes the content `.lock` files',
'command' => static function (CLI $cli): void {
$path = $cli->kirby()->root('content');
$directoryIterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directoryIterator);
$counter = 0;

foreach ($iterator as $file) {
if ($file->getFilename() === '.lock') {
F::remove($file->getPathName());
$counter++;
}
}

$cli->success($counter . ' lock file(s) have been deleted');
}
];
15 changes: 15 additions & 0 deletions commands/clear/logins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\F;

return [
'description' => 'Deletes the users `.logins` file',
'command' => static function (CLI $cli): void {
F::remove($cli->kirby()->root('accounts') . '/.logins');

$cli->success('The .logins file has been deleted');
}
];
2 changes: 1 addition & 1 deletion commands/make/_templates/model.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

class {{ className }}Page extends Page {
class {{ className }}Page extends Kirby\Cms\Page {

}
44 changes: 44 additions & 0 deletions commands/make/language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Cms\Language;

return [
'description' => 'Creates a new language',
'args' => [
'code' => [
'description' => 'The code of the language'
],
'name' => [
'description' => 'The name of the language'
],
'locale' => [
'description' => 'The locale of the language'
],
'direction' => [
'description' => 'The direction of the language'
]
],
'command' => static function (CLI $cli): void {
$kirby = $cli->kirby();
$code = $cli->argOrPrompt('code', 'Enter a language code:');
$name = $cli->argOrPrompt('name', 'Enter a language name (optional):', false);
$locale = $cli->argOrPrompt('locale', 'Enter a language locale (optional):', false);
$direction = $cli->radio('Select language direction:', ['ltr', 'rtl'])->prompt();

// authenticate as almighty
$kirby->impersonate('kirby');

Language::create([
'code' => $code,
'name' => empty($name) === false ? $name : $code,
'locale' => $locale,
'direction' => $direction,
'default' => $kirby->languages()->count() === 0,
]);

$cli->success('The language has been created');
}
];
53 changes: 53 additions & 0 deletions commands/make/user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;
use Kirby\Cms\User;

return [
'description' => 'Creates a new user',
'args' => [
'email' => [
'description' => 'The email of the user'
],
'role' => [
'description' => 'The role of the user'
],
'name' => [
'description' => 'The name of the user'
],
'language' => [
'description' => 'The language of the user',
],
'password' => [
'description' => 'The password of the user'
]
],
'command' => static function (CLI $cli): void {
$kirby = $cli->kirby();
$email = $cli->argOrPrompt('email', 'Enter an email:');
$password = $cli->argOrPrompt('password', 'Enter a password (Leave empty for passwordless login methods):', false);
$role = $cli->radio('Select a user role:', $kirby->roles()->pluck('id'))->prompt();
$name = $cli->argOrPrompt('name', 'Enter a name (optional):', false);
$language = $cli->argOrPrompt('language', 'Enter a language code (Leave empty to use default EN):', false);

$data = [
'email' => $email,
'name' => $name,
'role' => $role,
'language' => empty($language) === false ? strtolower($language) : 'en'
];

if (empty($password) === false) {
$data['password'] = $password;
}

// authenticate as almighty
$kirby->impersonate('kirby');

$user = User::create($data);

$cli->success('The user has been created. The new user id: ' . $user->id());
}
];
Loading

0 comments on commit 8cb3477

Please sign in to comment.