-
Notifications
You must be signed in to change notification settings - Fork 118
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 #1012 from M0rgan01/delete-backup-command
[CLI] Create "backup:delete" command
- Loading branch information
Showing
11 changed files
with
227 additions
and
74 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
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,112 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright since 2007 PrestaShop SA and Contributors | ||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0) | ||
* that is bundled with this package in the file LICENSE.md. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/AFL-3.0 | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | ||
* versions in the future. If you wish to customize PrestaShop for your | ||
* needs please refer to https://devdocs.prestashop.com/ for more information. | ||
* | ||
* @author PrestaShop SA and Contributors <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) | ||
*/ | ||
|
||
namespace PrestaShop\Module\AutoUpgrade\Commands; | ||
|
||
use DateTime; | ||
use Exception; | ||
use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder; | ||
use PrestaShop\Module\AutoUpgrade\Backup\BackupManager; | ||
use PrestaShop\Module\AutoUpgrade\Exceptions\BackupException; | ||
use PrestaShop\Module\AutoUpgrade\UpgradeContainer; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ChoiceQuestion; | ||
|
||
abstract class AbstractBackupCommand extends AbstractCommand | ||
{ | ||
/** @var BackupFinder */ | ||
protected $backupFinder; | ||
|
||
/** @var BackupManager */ | ||
protected $backupManager; | ||
|
||
protected function setupEnvironment(InputInterface $input, OutputInterface $output): void | ||
{ | ||
parent::setupEnvironment($input, $output); | ||
$this->backupFinder = new BackupFinder($this->upgradeContainer->getProperty(UpgradeContainer::BACKUP_PATH)); | ||
$this->backupManager = new BackupManager($this->backupFinder); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
protected function selectBackupInteractive(InputInterface $input, OutputInterface $output): ?string | ||
{ | ||
$backups = $this->backupFinder->getAvailableBackups(); | ||
|
||
if (empty($backups)) { | ||
$this->logger->info('No store backup files found in your dedicated directory'); | ||
|
||
return null; | ||
} | ||
|
||
$formattedBackups = array_map(function ($backupName) { | ||
return $this->backupFinder->parseBackupMetadata($backupName); | ||
}, $backups); | ||
|
||
$this->backupFinder->sortBackupsByNewest($formattedBackups); | ||
|
||
$rows = array_map(function ($backup) { | ||
return $this->formatBackupRow($backup); | ||
}, $formattedBackups); | ||
|
||
$exit = 'Exit the process'; | ||
$rows[] = $exit; | ||
|
||
$helper = $this->getHelper('question'); | ||
$question = new ChoiceQuestion( | ||
'Please select your backup:', | ||
$rows | ||
); | ||
|
||
$answer = $helper->ask($input, $output, $question); | ||
|
||
if ($answer === $exit) { | ||
return null; | ||
} | ||
|
||
$key = array_search($answer, $rows); | ||
if ($key === false) { | ||
throw new BackupException('Invalid backup selection.'); | ||
} | ||
|
||
return $formattedBackups[$key]['filename']; | ||
} | ||
|
||
/** | ||
* Formats a backup row for display in the selection prompt. | ||
* | ||
* @param array{datetime: string, version:string, filename: string} $backups | ||
* | ||
* @return string | ||
*/ | ||
private function formatBackupRow(array $backups): string | ||
{ | ||
return sprintf('Date: %s, Version: %s, File name: %s', $backups['datetime'], $backups['version'], $backups['filename']); | ||
} | ||
} |
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
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,90 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright since 2007 PrestaShop SA and Contributors | ||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0) | ||
* that is bundled with this package in the file LICENSE.md. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/AFL-3.0 | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | ||
* versions in the future. If you wish to customize PrestaShop for your | ||
* needs please refer to https://devdocs.prestashop.com/ for more information. | ||
* | ||
* @author PrestaShop SA and Contributors <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) | ||
*/ | ||
|
||
namespace PrestaShop\Module\AutoUpgrade\Commands; | ||
|
||
use Exception; | ||
use InvalidArgumentException; | ||
use PrestaShop\Module\AutoUpgrade\Task\ExitCode; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class DeleteBackupCommand extends AbstractBackupCommand | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected static $defaultName = 'backup:delete'; | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Delete a store backup file.') | ||
->setHelp( | ||
'This command allows you to delete a store backup file.' | ||
) | ||
->addArgument('admin-dir', InputArgument::REQUIRED, 'The admin directory name.') | ||
->addOption('backup', null, InputOption::VALUE_REQUIRED, 'Specify the backup name to delete. The allowed values can be found with backup:list command)'); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): ?int | ||
{ | ||
try { | ||
$this->setupEnvironment($input, $output); | ||
|
||
$backup = $input->getOption('backup'); | ||
$exitCode = ExitCode::SUCCESS; | ||
|
||
if (!$backup) { | ||
if (!$input->isInteractive()) { | ||
throw new InvalidArgumentException("The '--backup' option is required."); | ||
} | ||
|
||
$backup = $this->selectBackupInteractive($input, $output); | ||
|
||
if (!$backup) { | ||
return $exitCode; | ||
} | ||
} | ||
|
||
$this->backupManager->deleteBackup($backup); | ||
$this->logger->info('The backup file has been successfully deleted'); | ||
|
||
$this->logger->debug('Process completed with exit code: ' . $exitCode); | ||
|
||
return $exitCode; | ||
} catch (Exception $e) { | ||
$this->logger->error('An error occurred during the delete backup process'); | ||
throw $e; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.