Skip to content

Commit

Permalink
Merge pull request #1012 from M0rgan01/delete-backup-command
Browse files Browse the repository at this point in the history
[CLI] Create "backup:delete" command
  • Loading branch information
M0rgan01 authored Nov 21, 2024
2 parents b31fa33 + 051ac3f commit 35d1c7e
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 74 deletions.
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand;
use PrestaShop\Module\AutoUpgrade\Commands\CreateBackupCommand;
use PrestaShop\Module\AutoUpgrade\Commands\DeleteBackupCommand;
use PrestaShop\Module\AutoUpgrade\Commands\ListBackupCommand;
use PrestaShop\Module\AutoUpgrade\Commands\RestoreCommand;
use PrestaShop\Module\AutoUpgrade\Commands\UpdateCommand;
Expand All @@ -52,6 +53,7 @@ $application->add(new RestoreCommand());
$application->add(new CheckRequirementsCommand());
$application->add(new CreateBackupCommand());
$application->add(new ListBackupCommand());
$application->add(new DeleteBackupCommand());

$input = new ArgvInput();
$output = new ConsoleOutput();
Expand Down
2 changes: 1 addition & 1 deletion classes/Backup/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function deleteBackup(string $backupName): void

$filesystem = new Filesystem();
$filesystem->remove([
$this->backupFinder->getBackupPath() . DIRECTORY_SEPARATOR . BackupFinder::BACKUP_ZIP_NAME_PREFIX . $backupName,
$this->backupFinder->getBackupPath() . DIRECTORY_SEPARATOR . BackupFinder::BACKUP_ZIP_NAME_PREFIX . $backupName . '.zip',
$this->backupFinder->getBackupPath() . DIRECTORY_SEPARATOR . $backupName,
]);

Expand Down
112 changes: 112 additions & 0 deletions classes/Commands/AbstractBackupCommand.php
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']);
}
}
2 changes: 1 addition & 1 deletion classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class AbstractCommand extends Command
/**
* @throws Exception
*/
protected function setupContainer(InputInterface $input, OutputInterface $output): void
protected function setupEnvironment(InputInterface $input, OutputInterface $output): void
{
$this->logger = new CliLogger($output);
if ($output->isQuiet()) {
Expand Down
4 changes: 3 additions & 1 deletion classes/Commands/CheckRequirementsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use PrestaShop\Module\AutoUpgrade\Services\DistributionApiService;
use PrestaShop\Module\AutoUpgrade\Services\PhpVersionResolverService;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeSelfCheck;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
try {
$this->setupContainer($input, $output);
$this->setupEnvironment($input, $output);
$this->output = $output;

$configPath = $input->getOption('config-file-path');
Expand All @@ -79,6 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int

$this->upgradeContainer->initPrestaShopAutoloader();
$this->upgradeContainer->initPrestaShopCore();
$this->upgradeContainer->getState()->initDefault($this->upgradeContainer->getProperty(UpgradeContainer::PS_VERSION), $this->upgradeContainer->getUpgrader()->getDestinationVersion());

$distributionApiService = new DistributionApiService();
$phpVersionResolverService = new PhpVersionResolverService(
Expand Down
2 changes: 1 addition & 1 deletion classes/Commands/CreateBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
try {
$this->setupContainer($input, $output);
$this->setupEnvironment($input, $output);

$controller = new AllBackupTasks($this->upgradeContainer);
$controller->init();
Expand Down
90 changes: 90 additions & 0 deletions classes/Commands/DeleteBackupCommand.php
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;
}
}
}
20 changes: 8 additions & 12 deletions classes/Commands/ListBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
namespace PrestaShop\Module\AutoUpgrade\Commands;

use Exception;
use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder;
use PrestaShop\Module\AutoUpgrade\Exceptions\BackupException;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListBackupCommand extends AbstractCommand
class ListBackupCommand extends AbstractBackupCommand
{
/** @var string */
protected static $defaultName = 'backup:list';
Expand All @@ -56,19 +54,17 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
try {
$this->setupContainer($input, $output);
$this->setupEnvironment($input, $output);

$backupPath = $this->upgradeContainer->getProperty(UpgradeContainer::BACKUP_PATH);
$backupFinder = new BackupFinder($backupPath);
$backups = $backupFinder->getAvailableBackups();
$backups = $this->backupFinder->getAvailableBackups();

if (empty($backups)) {
$this->logger->info('No store backup files found in your dedicated directory');

return ExitCode::SUCCESS;
}

$rows = $this->getRows($backupFinder, $backups);
$rows = $this->getRows($backups);
$table = new Table($output);
$table
->setHeaders(['Date', 'Version', 'File name'])
Expand All @@ -90,13 +86,13 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
*
* @throws BackupException
*/
private function getRows(BackupFinder $backupFinder, array $backups): array
private function getRows(array $backups): array
{
$rows = array_map(function ($backupName) use ($backupFinder) {
return $backupFinder->parseBackupMetadata($backupName);
$rows = array_map(function ($backupName) {
return $this->backupFinder->parseBackupMetadata($backupName);
}, $backups);

$backupFinder->sortBackupsByNewest($rows);
$this->backupFinder->sortBackupsByNewest($rows);

foreach ($rows as &$row) {
unset($row['timestamp']);
Expand Down
Loading

0 comments on commit 35d1c7e

Please sign in to comment.