-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from getkirby/develop
Kirby CLI 1.4.0
- Loading branch information
Showing
16 changed files
with
527 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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,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))); | ||
} | ||
]; |
This file contains 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 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,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'); | ||
} | ||
]; |
This file contains 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 @@ | ||
<?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'); | ||
} | ||
]; |
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
|
||
class {{ className }}Page extends Page { | ||
class {{ className }}Page extends Kirby\Cms\Page { | ||
|
||
} |
This file contains 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,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'); | ||
} | ||
]; |
This file contains 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,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()); | ||
} | ||
]; |
Oops, something went wrong.