|
| 1 | +<?php |
| 2 | +namespace Breadlesscode\Backups\Command; |
| 3 | + |
| 4 | +use Neos\Flow\Cli\CommandController; |
| 5 | +use Neos\Flow\Annotations as Flow; |
| 6 | + |
| 7 | +use Breadlesscode\Backups\Service\BackupService; |
| 8 | + |
| 9 | +class BackupCommandController extends CommandController |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @Flow\Inject() |
| 13 | + * @var BackupService |
| 14 | + */ |
| 15 | + protected $backupService; |
| 16 | + |
| 17 | + /** |
| 18 | + * creates a single backup of a specific package |
| 19 | + */ |
| 20 | + public function createCommand(): void |
| 21 | + { |
| 22 | + if ($this->backupService->noStepsConfigured()) { |
| 23 | + $this->outputError('No backup steps configured. Please configure Breadlesscode.Backups.steps'); |
| 24 | + $this->quit(); |
| 25 | + } |
| 26 | + |
| 27 | + $this->output('Creating backup...'); |
| 28 | + $this->backupService->createBackup(); |
| 29 | + $this->outputLine('<success>success</success>'); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * restores a single backup of a specific package |
| 34 | + */ |
| 35 | + public function restoreCommand(string $name, bool $noConfirm = false): void |
| 36 | + { |
| 37 | + $shouldRestore = true; |
| 38 | + $backup = $this->backupService->getBackup($name); |
| 39 | + $steps = $this->backupService->getStepsInstances( |
| 40 | + $this->backupService->getTemporaryBackupPath(), |
| 41 | + $backup['meta']['steps'] |
| 42 | + ); |
| 43 | + // print warnings |
| 44 | + foreach ($steps as $stepClass => $step) { |
| 45 | + /** @var $step StepInterface */ |
| 46 | + $message = $step->getRestoreWarning(); |
| 47 | + |
| 48 | + if($message !== null) { |
| 49 | + $this->outputLine('<error>'.$stepClass.'</error>'); |
| 50 | + $this->outputLine('<comment>'.$message.'</comment>'); |
| 51 | + $this->outputLine(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if ($noConfirm === false) { |
| 56 | + $shouldRestore = $this->output->askConfirmation('Are you sure you want to restore this Backup?', false); |
| 57 | + } |
| 58 | + |
| 59 | + if(!$shouldRestore) { |
| 60 | + $this->outputLine(); |
| 61 | + $this->outputLine('<error>Canceled by user</error>'); |
| 62 | + $this->quit(); |
| 63 | + } |
| 64 | + |
| 65 | + $this->output('Restoring backup...'); |
| 66 | + try { |
| 67 | + $this->backupService->restoreBackup($name); |
| 68 | + $this->outputLine('<success>success</success>'); |
| 69 | + } catch (\Exception $e) { |
| 70 | + $this->outputError($e->getMessage()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * lists all backups |
| 76 | + */ |
| 77 | + public function listCommand($offset = 0, $limit = 60): void |
| 78 | + { |
| 79 | + $backups = $this->backupService->getBackups($offset, $limit); |
| 80 | + |
| 81 | + $backups = array_map(function($backup) { |
| 82 | + unset($backup['meta']); |
| 83 | + return $backup; |
| 84 | + }, $backups); |
| 85 | + |
| 86 | + $this->outputLine(); |
| 87 | + $this->output->outputTable($backups, ['Name', 'Date', 'Relative date']); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * deletes backups |
| 92 | + */ |
| 93 | + public function deleteCommand(string $name, bool $noConfirm = false): void |
| 94 | + { |
| 95 | + $confirmed = true; |
| 96 | + |
| 97 | + if (!$noConfirm) { |
| 98 | + $confirmed = $this->output->askConfirmation('Are you sure you want to delete this backup?'); |
| 99 | + } |
| 100 | + |
| 101 | + if (!$confirmed) { |
| 102 | + $this->outputLine(); |
| 103 | + $this->outputLine('<error>Canceled by user</error>'); |
| 104 | + $this->quit(); |
| 105 | + } |
| 106 | + |
| 107 | + $this->output('Deleting backup...'); |
| 108 | + |
| 109 | + try { |
| 110 | + $this->backupService->deleteBackup($name); |
| 111 | + $this->outputLine('<success>success</success>'); |
| 112 | + } catch (\Exception $e) { |
| 113 | + $this->outputError($e->getMessage()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + protected function outputError(string $message): void |
| 118 | + { |
| 119 | + $this->outputLine('<error>failed</error>'); |
| 120 | + $this->outputLine(); |
| 121 | + $this->outputLine('Reason:'); |
| 122 | + $this->outputLine($message); |
| 123 | + } |
| 124 | +} |
0 commit comments